javascript – What does the "=>" operator mean?

Question:

I was looking at some JavaScript solutions and in one case I saw this command line: return args.reduce((s, v) => s + v, 0); . But I don't know what the => operator means.

What is his role?

Answer:

This is known as Arrow functions .
An Arrow function is just like a normal function/callback, only less verbose and instance references like this are taken from the "surroundings" (which avoids .bind() or those var that=this ).

Then:

var numbers = [1,2,3];
squares = numbers.map(x => x * x);

which is equivalent to:

squares = numbers.map(function (x) { return x * x });

I don't want to write a long answer with all the details because that's redundant. There's so much about it out there that it's not worth the effort. As an example, here is some EXCELLENT content about it:

http://exploringjs.com/es6/ch_arrow-functions.html

Scroll to Top