Are these 2 JavaScript statements equivalent?

73 Views Asked by At

These 2 statements seem to do the same thing.

const handleClick = () => alert('foo');

and

function handleClick() {
    alert('foo');
}

Are they identical and just syntactically different? The first one looks like a variable declaration, whereas the second one is clearly a function definition.

2

There are 2 best solutions below

2
On

No, they are not the same thing. The arrow function has some limitations:

  • Does not have its own bindings to this or super, and should not be used as methods.
  • Does not have new.target keyword.
  • Not suitable for call, apply and bind methods, which generally rely on establishing a scope.
  • Can not be used as constructors.
  • Can not use yield, within its body.
0
On

ES6 arrow functions provide you with an alternative way to write a shorter syntax compared to the function expression. There is no performance difference.

Are arrow functions faster (more performant, lighter) than ordinary standalone function declaration in v8?