ES6 - Use Arrow Functions to Write Concise Anonymous Functions

Tell us what’s happening:
I am getting the following

Failed:magic() should return the correct date.

The code seems fine but not sure why I m getting the error.

Your code so far

const magic = () => { new Date() };

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: ES6 - Use Arrow Functions to Write Concise Anonymous Functions

Link to the challenge:

You deleted the return keyword.

Let me rewrite that to be more clear:

const magic = () => {
  new Date();
};

You’ve chosen to wrap the body of the function in a code block, wrapping it in curly braces, { and }. You always do that with standard functions, but with arrow functions it is optional. If you do that, then you must specify the return. If you have only one line that evaluates to what you want, you can leave off the curly braces and the return.

These two functions do the same thing:

const doubleIt = num => { return num * 2; };

and

const doubleIt = num => num * 2; 
2 Likes

es6 return can be implicit. so no return is optional

The return statement is mandatory if you have {}. The implicit return only occurrs without the {}.

2 Likes

Thank you for the explanation, for first I thought maybe it was timezones :sweat_smile: