Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 double arrow parameters (i.e. const update = x => y => { } ) [duplicate]

What does double arrow parameters mean in the following code?

const update = x => y => {
   // Do something with x and y
}

How is it different compared to the following?

const update = (x, y) => {
   // Do something with x and y
}

Thanks!

like image 902
His Avatar asked Sep 22 '17 12:09

His


People also ask

What does => mean in ES6?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

What does => mean in programming?

What It Is. This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be used similarly to the way you would use function expressions. In other words, you can often use them in place of expressions like function (foo) {...} .

How are arrow functions () => {} different than traditional function expressions?

Unlike regular functions, arrow functions do not have their own this . The value of this inside an arrow function remains the same throughout the lifecycle of the function and is always bound to the value of this in the closest non-arrow parent function.

Is arrow function supported in IE11?

Arrow functions are not supported in IE11 or earlier.


1 Answers

Let's rewrite them "old style", the first one is:

const update = function (x) {
  return function(y) {
    // Do something with x and y
  };
};

While the second one is:

const update = function (x, y) {
  // Do something with x and y
};

So as you can see they are quite different, the first returns an "intermediate" function, while the second is a single function with two parameters.

like image 154
Matteo Tassinari Avatar answered Oct 13 '22 19:10

Matteo Tassinari