What does x => x mean when passed to a function in Javascript?

86 Views Asked by At

Say I have code like this:

function Foo(func) 
{
    var a = new SomeClass(func(5));
}
var b = new Foo(x=>x);

What does the x => x mean in the parameter? x is not defined anywhere else.

2

There are 2 best solutions below

0
On

It is the arrow notation,

x=>x

implies function which takes one parameter and returns back the same parameter.

It is the same as:-

function test(x) {
  return x;
}
var b = new Foo(test);
0
On

As @Hozefa said, the function accepts the parameter x and returns it back.

Basically:

const func = x => x

means:

const func = function (x) {
    return x
}

It's an ES6 Syntax which you learn more here: http://es6-features.org/