Skip to content
Advertisement

Why does it print “undefined” on the console? [closed]

I have the following JavaScript code:

please help

let statement = ['Kim is a good, kind and smart boy'];
    
let kim = {
  message () {
    console.log(statement);
  },
  interest () {
    console.log('sports');
  }
}
console.log(kim.message());
console.log(kim.interest());

I expect that it should print this output:

[ 'Kim is a good, kind and smart boy' ]
sports

But instead, it prints the following:

[ 'Kim is a good, kind and smart boy' ]
undefined
sports
undefined

Why does it print “undefined” after each function within the method?

Advertisement

Answer

I suppose you’re calling console.log(kim.message()); and console.log(kim.interest());

console.log prints the return value of the passed function but because both the function don’t have a return statement console.log prints undefined that’s way you see those additional undefined logs.

Advertisement