console.log(myFunction()) returns undefined

In JavaScript, if nothing is returned from the function with the keyword return then undefined is returned by default.

var data = greet();
console.log(data);// undefined, since your function does not return.

Is equivalent to:

console.log(greet());

The second output is the returned result from the function. Since you are not returning anything from the function hence prints undefined.

To print ‘Hi’ in the second console you have to return that from the function.

function greet() {
  console.log("Hi");
  return 'Hi';
};

console.log(greet());

Leave a Comment