Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused about return array#map javascript [duplicate]

I have the 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 while func2 return good value (that i need)?

Sorry for my english.

like image 256
mqklin Avatar asked May 17 '15 07:05

mqklin


1 Answers

Internally in JS engine first example looks like this,

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

that's why you get undefined, don't add new line after return, because return statement followed by a new line tells the JS intepreter that a semi colon should be inserted after that return.

function func1() {
  return array.map(function(el){
     return el.method();
  });
};
like image 162
Oleksandr T. Avatar answered Nov 13 '22 15:11

Oleksandr T.