I have the following JavaScript code:
please help
JavaScript
x
13
13
1
let statement = ['Kim is a good, kind and smart boy'];
2
3
let kim = {
4
message () {
5
console.log(statement);
6
},
7
interest () {
8
console.log('sports');
9
}
10
}
11
console.log(kim.message());
12
console.log(kim.interest());
13
I expect that it should print this output:
JavaScript
1
3
1
[ 'Kim is a good, kind and smart boy' ]
2
sports
3
But instead, it prints the following:
JavaScript
1
5
1
[ 'Kim is a good, kind and smart boy' ]
2
undefined
3
sports
4
undefined
5
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.