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 trialRoald Jurrian Kamman
Front End Web Development Techdegree Graduate 15,544 Pointsis val a keyword or is the arrow function pointing at itself? Arrow function syntax looks weird.
Hey guys/girls/everyoneinbetweenabovetothesideupdownunderover.
This arrow function syntax doesn't seem right to me. And I want to know why it works.
const breakfastPromise = new Promise( (reslove, reject) => {
setTimeout(() => {
resolve('Your order is ready.');
}, 3000);
});
// is val the name of the arrow function? Pointing at itself? Since when can you use => //without the = (). (except for one line. But even on one line I was never tought that you can simply use => in certain cases.
// why is setTimeout setTimeout(() => and not: setTimeout( => ?
breakfastPromise.then( val => console.log(val) );
It feels like every video the syntax is different. I have noticed Guil doing this before and I just didn't have a good analogy yet.
It's like having a Spanish teacher trying to teach you a new word. But instead of using the new word in a sentence with words you already know the teacher just speaks 10 words which you have never learned yet. How am I supposed to even...?
I have days where I can run through 60 tasks on treehouse and then there is those days where I watch 2 videos and spend 4-6 hours figuring out what the f*** those other 9 words are so that I can understand the sentence.
Just stick to the same syntax for some time... please.
1 Answer
Steven Parker
231,184 PointsAn arrow function definition always starts with the list of parameters. This list is usually enclosed in parentheses but they can be omitted when the function takes one single parameter. Examples:
() => { /* function body */ } // this function takes no parameters
myparm => { /* function body */ } // this function takes ONE parameter
(one, two) => { /* function body */ } // this function takes two parameters
These are all "long form" arrow functions. There's also a short form that doesn't have a body in braces but instead has a single expression which is evaluated and then returned as the function's value. There's an example of that style on the last line of your original code above, but it's being used for convenience since the return value will be ignored (and is "undefined" anyway).
JASON LEE
17,352 PointsJASON LEE
17,352 PointsI hear you man it's frustrating. I try to think of it like this -- there are soo many programming languages. In our career we will probably learn more than one programming language, each with variations in their own syntax. While the inconsistencies can be a distraction, I try to keep my focus on the underlying principles. We know that
=>
means there's a function coming into play. Also, if there are multiple syntax to make the same statement i.e. concatenating with" " + " "
vs${} ` template literal, I try to use them both interchangeably. Because when you work professionally with other developers, chances are you will see code written in slightly different syntax variations than you're used to, so best to get comfortable with it.