In this statement you call the greeting
function. The function prints the greeting as specified inside of its code body. Since you don’t have a return
statement in the function, the function implicitly returns 'undefined'
to the caller. In this case the caller is console.log()
, so 'undefined'
gets logged to the console after your greeting. To fix this behavior you can do one of two things. Either change your console.log()
statements inside your function to return
the greeting, or just invoke the function without the console.log()
like so: greeting(userName)
.
let userName = 'Joe';
function greeting (userName){
if(userName !== ''){
console.log('Hello '+ userName + '!'); //change console.log to: return
} else {
console.log('Hello!') //same thing here
}
};
console.log(greeting(userName)); //Or change to: greeting(userName);