Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

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

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

console.log(haha());

Solution 2:

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.

Post a Comment for "A Function With A Console Log Output Inside Console.log Prints Undefined"