3

Sometimes I want to assign a variable the result of an Arrow function, but if I assign the Arrow Function to that variable it get the function and not the return of that Function as is obvious.

let theNumber2 = ()=>{return 2};

theNumber2 will contain the function and only if I do theNumber2() I will get returned 2.

Is there a way to do something like this to get directly the return of the arrow function? For example:

let theNumber2 = ()=>{return 2}(); // Note the () at the final

Does there exist something to do that? I mean, to assign the arrow function called?

Daniel F
  • 13,684
  • 11
  • 87
  • 116
SmoggeR_js
  • 2,950
  • 4
  • 22
  • 52

1 Answers1

9

Just wrap in parenthesis and call the function.

let theNumber2 = (() => { return 2; })();

console.log(theNumber2);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 3
    Side note: `let theNumber2 = (() => 2)();` ;-) – T.J. Crowder Jan 28 '19 at 11:12
  • mmm, if value of "2" changes between declaration of `theNumber2` and `console.log(theNumber2)`, it will still show `2` right? – Hammerbot Jan 28 '19 at 11:13
  • @Hammerbot yes it will still show 2 – Manos Kounelakis Jan 28 '19 at 11:14
  • @T.J.Crowder I would prefer the return statement.The way you wrote it its smaller however in a quick glance if someone looked at the code they might got confused – Manos Kounelakis Jan 28 '19 at 11:16
  • Nice, I didn't think about it!. I need 9 min to make this the correct answer. Thanks a lot. – SmoggeR_js Jan 28 '19 at 11:16
  • @ManosKounelakis - The concise form is **very** widely-used. Consider: `result = original.map(x => x.foo);` Getting used to `=>` not followed by `{` meaning it's returning what follows is going to be important. :-) – T.J. Crowder Jan 28 '19 at 11:22
  • 1
    @Hammerbot - `let x = 2; let theNumber2 = (() => x)(); x = 3; console.log(theNumber2);` will still show `2` for the same reason this still shows `2`: `let x = 2; let y = x; x = 3; console.log(y);` The **value** is returned, with no enduring link to where that value came from. – T.J. Crowder Jan 28 '19 at 11:24
  • @T.J.Crowder thanks for the reply :).I use the same form as you .What I say is that if I read it as a newbie ,I am not sure I would have figured out what this code does from the first glance – Manos Kounelakis Jan 28 '19 at 11:24
  • 1
    @ManosKounelakis but you only need to learn what `() => 2` does once. If we start avoiding the more convenient syntax because somebody who never encountered it would not spend the 30 seconds needed to understand it for the rest of their lives, then we should just stop all progress and never add any language features to anything ever. Because there would always be newbies. Heck, we were all newbies. Yet we not only manage to push through the new development but actually push for them. The arrow-function shorthand was introduced *because* people wanted it and was more convenient. – VLAZ Jan 28 '19 at 11:43
  • @vlaz, *" ... we were all newbies ..."* o_O – Nina Scholz Jan 28 '19 at 11:49
  • @NinaScholz weren't we? I don't know of a single person who was born with the entire knowledge for programming in their brains. Yet we learned and started using more advanced contructs and teaching others so they can do the same. – VLAZ Jan 28 '19 at 11:53