Arrow function vs function declaration

Question

What are the differences between the arrow function and the function expression?

Answer

Declaring them:

const sayName = (name) => {
   console.log(name);
}
//vs
function sayName(name) {
   console.log(name);
}

objects:

const obj = {
  getName: function() {
    // ...
  }
};
//vs
const obj = {
  getName: () => {
    // ...
  }
};

Callbacks:

setTimeout(function() {
  // ...
}, 500);

// vs

setTimeout(() => {
  // ...
}, 500);

Arrow functions came in the ECMAscript version 6

2 Likes

This information is not complete. More on this subject can be found here on the documentation.

It is appreciated that you want to share information, but keep in mind that a student should never act to be a teacher until he has mastered the arts. You will usually do more harm than good.

I advice to leave these kind of topics to the Codecadamy moderators.

If this is a post where you want verification if your statement is correct you should word it as so.

1 Like

Further to what @biirra has already said.

We of course love the community posting their own guides and sharing knowledge. It is just important this knowledge is correct and given in full. Have a look at the documentation biira linked and try and rewrite your post to include the extra details that are currently missing.

4 Likes