Jamie Hutber
Jamie Hutber

Reputation: 28074

How to use a Arrow Function

I'm just reading up on ES6 features that have been implemented in Node v4.0.0 and saw Arrows. The example from Arrow Functions is:

var a = [
    "Hydrogen",
    "Helium",
    "Lithium",
    "Beryl­lium"
];
var a2 = a.map(function(s){ return s.length });
var a3 = a.map( s => s.length );

My question is how can I include multiple lines of code inside of a.map( s => s.length ); rather than just returning the length as in this example.

Upvotes: 2

Views: 111

Answers (1)

Sergey Rybalkin
Sergey Rybalkin

Reputation: 3026

Just wrap your multiple code lines in curly braces like this:

var a3 = a.map( s => {
    var temp = s.length;
    return temp;
});

Upvotes: 8

Related Questions