Seeking clarification on why use Arrow function notation

I am looking at this code.

const logSkyColor = () => {  
let color = 'blue';   console.log(color); // blue 
}

And I think it would be less typing if it was a function declaration.

function logSkycolor() {
let color = 'blue'; console.log(color);//blue
}

So beside sometimes looking neater - is there any real advantage to Arrow notation?

Like you said typing less but also you can save it to a variable. And being about to save it to a variable is a huge advantage depending on what you are using it for.

1 Like

For starters, with the second function example (the declaration), enter this line after the function:

logSkycolor = logSkycolor()

console.log(typeof logSkycolor)

undefined? That is what was returned from the function just before it got overwritten. Oops!

We cannot overwrite the first example because it is constant. If we want to use the standard function syntax, then write it as an expression…

const logSkycolor = function () {
    let color = 'blue';   console.log(color);
}

At this stage it’s pointless to look for advantages. Learn how to use the syntax, and get to know how things work. In time you can decide for yourself if there is any advantage to the new syntax. Wait until you get into Higher Order Functions and begin using callbacks.

Great answer.
Thank you

1 Like