A Mind United A Mind United - 5 months ago 11
Javascript Question

The use of brackets in javascript fat arrow function declaration

In ES6/ES2015 fat arrow functions can be declared with or without brackets around the parameter.

ie:

var foo_1 = myVar => {
return myVar + 1;
}


or:

var foo_2 = (myVar) => {
return myVar + 2;
}


What I would like to know is:
what is the difference (if any)?

Answer

There is no differences in your example.

You need parentheses if you

  • have no parameter: () => ...
  • have multiple parameters: (foo, bar) => ...
  • use destructuring: ({foo}) => ...
  • use default values: (foo = 42) => ...
  • have a rest parameter: (...bar) => ...
  • have any combination of the above

In other words, whenever you do not have a single identifier-only parameter.

Comments