Search code examples
javascriptecmascript-6arrow-functions

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.


Solution

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

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