Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops Working with 'for' Loops The Refactor Challenge – Duplicate Code

why does using an arrow function work but not a function declaration?

I tried to use a function declaration in place of the arrow function and the program didn't run. why doesn't it work?

2 Answers

Hi Ahmad!

If I understand your question properly, I think the issue comes down to arrow function shorthand syntax as explained here:

https://teamtreehouse.com/library/javascript-functions/arrow-functions/concise-arrow-function-syntax#arrow-functions-as-one-line-statements

To work, this:

const square = num => (num * num) ;

Would need to be converted to this (as a regular function declaration):

const square = function(num) { return (num * num) };

(the return being the key factor in the second code block)

I hope that helps.

Stay safe and happy coding!

I understand the syntax of an arrow function. my question is pertaining to something else but thanks for the help though.

Courtney Wilson
Courtney Wilson
8,031 Points

Your function getColor() isn't returning anything:

function getColor() {
  Math.floor(Math.random() * 256);
}

Add a return statement to return the random color value for it to work:

function getColor() {
  return Math.floor(Math.random() * 256);
}