简体   繁体   中英

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

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

console.log(haha());

Prints:

haha
undefined

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?

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

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM