2

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.

2
  • 4
    There are plenty of examples and details in the link you provided, even for multiple lined function, I don't see what can be added here. What exactly are you looking for that you did not find in the doc? Commented Sep 11, 2015 at 13:34
  • 2
    It's right there in the page you linked to at the very top: // Basic syntax: (param1, param2, paramN) => { statements } (param1, param2, paramN) => expression // equivalent to: => { return expression; } Commented Sep 11, 2015 at 13:39

1 Answer 1

8

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

var a3 = a.map( s => {
    var temp = s.length;
    return temp;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thats easier than I could ever have dreamed. I am looking forward to it.
…and don't forget that the return is no more implicit then, right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.