A function with a console log output inside console.log prints undefined

后端 未结 2 1797
耶瑟儿~
耶瑟儿~ 2021-01-29 03:22
function haha(){
  console.log(\'haha\');
}

console.log(haha());

Prints:

haha
undefined

Is it because if you don\'t

相关标签:
2条回答
  • 2021-01-29 04:08

    Is it because if you don't specify return in a function it will return undefined and this is what the second console.log is outputting?

    Yes, console.log() prints the arguments passed to it. Arguments passed to it in the second time is the returned value of haha() which is undefined

    First console log is the due to the console.log inside haha.

    0 讨论(0)
  • 2021-01-29 04:17

    It returns undefined because basically your function doesn't return anything.

    You could e.g. return a "haha" string or whatever you like.

    function haha(){
      console.log('haha');
      return 'haha';
    }
    
    console.log(haha());

    0 讨论(0)
提交回复
热议问题