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 Debug Arrow Function Expressions

Sathvik Reddy
Sathvik Reddy
6,223 Points

Third (Last) Function

Isn't the least verbose way of writing the printDate() function as follows?

const printDate = date => console.log(date);

^since there is only one line of code in the function.

Your syntax shows:

const printDate = date => {
     console.log(date);
}
Andreas Nyström
Andreas Nyström
8,887 Points

One-upping the teachers ey? Well done :).

Sathvik Reddy
Sathvik Reddy
6,223 Points

Andreas Nyström - haha, wasn't trying to be a smarta**. Just wanted to make sure I was understanding arrow functions correctly ;)

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,696 Points

Question updated with code formatting. Check out the Markdown Cheatsheet link below the "Add an Answer" for syntax examples.

3 Answers

Steven Parker
Steven Parker
231,248 Points

There's a slight difference, the briefer form actually translates to this:

const printDate = date => {
    return console.log(date);
}

But in this case there's no practical difference since "console.log" doesn't return anything.

@Steven Parker , I find this weird since in the video you're explicitly told about refactoring on arrow functions that have only 1 line of code in the code block .The syntax supports that it can be on 1 line and no curly brackets are needed.

When arrow functions have 1 parameter no parentheses are needed either.

So wouldn't the ultimate refactor be

const printDate = date => console.log(date);

It's seems to work fine when outputted in the console. I am just wondering if I am missing something here.

Steven Parker
Steven Parker
231,248 Points

It works just fine, I was just pointing out that the equivalent conventional function would include a return.

But as I said, it's not important in this case since there's no value to return.

I got confused with the third function too, However if you made it here, it means you were paying attention. :)

same here. I think

const printDate = date => console.log(date);

would be the finished version in the context of this exercise.