6

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.

mqklin
  • 1,838
  • 4
  • 18
  • 35

1 Answers1

11

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();
  });
};
Oleksandr T.
  • 73,526
  • 17
  • 164
  • 143