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 trialSathvik Reddy
6,223 PointsThird (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);
}
Sathvik Reddy
6,223 PointsAndreas Nyström - haha, wasn't trying to be a smarta**. Just wanted to make sure I was understanding arrow functions correctly ;)
Rich Donnellan
Treehouse Moderator 27,696 PointsQuestion updated with code formatting. Check out the Markdown Cheatsheet link below the "Add an Answer" for syntax examples.
3 Answers
Steven Parker
231,269 PointsThere'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.
maikelvreugdenhil
1,762 Points@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
231,269 PointsIt 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.
pcachia
14,454 PointsI got confused with the third function too, However if you made it here, it means you were paying attention. :)
Florian Gröne
Front End Web Development Techdegree Graduate 14,493 Pointssame here. I think
const printDate = date => console.log(date);
would be the finished version in the context of this exercise.
Andreas Nyström
8,887 PointsAndreas Nyström
8,887 PointsOne-upping the teachers ey? Well done :).