Confused with return array # map javascript

I have a code:

function func1(){ return array.map(function(el){ return el.method(); }); } function func2(){ var confused = array.map(function(el){ return el.method(); }); return confused; } 

Why func1 return undefined and func2 returns a good value (what do I need)?

Sorry for my English.

+6
source share
1 answer

Internally, in the first example, the JS engine looks like this:

 function func1() { return; array.map(function(el){ return el.method(); }); }; 

why do you get undefined , do not add a new line after return , because the return statement, followed by a new line, tells JS-intepreter that after that return half a ton should be inserted.

 function func1() { return array.map(function(el){ return el.method(); }); }; 
+11
source

Source: https://habr.com/ru/post/987362/


All Articles