0

In a tutorial, the presenter used an unfamiliar syntax in an array.map function expression like this:

.map(x => x.trim());

map() takes a callback function, suggesting thet this expression is creating a function. Searching for "=>" here and generally on google returned no recognizable hits. Searching on symbols can often be difficult, but I can't think of a good way to learn what this is doing.

What is the meaning of this expression?

Adjit
  • 10,134
  • 12
  • 53
  • 98
user1488759
  • 103
  • 1
  • 6
  • [ES2015 arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – Pointy Oct 30 '15 at 18:29
  • My apologies for the duplicate. Searching on "=>" does not return the previous answer.. – user1488759 Oct 30 '15 at 19:49

3 Answers3

3

It's an arrow function:

An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.

From the doc, these two are equivalent:

a.map(function(s){ return s.length });
a.map( s => s.length );

This syntax was added to Javascript in ES 2015.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
Claudiu
  • 224,032
  • 165
  • 485
  • 680
0

These are lambda functions, ECMA defines them as arrow functions.

Arrow Functions

ergonaut
  • 6,929
  • 1
  • 17
  • 47
0

What you see is an arrow function which is only valid syntax in ES6. It is almost the same as saying .map(function(x) { return x.trim(); })

kliron
  • 4,383
  • 4
  • 31
  • 47