Godfrey Fernandes
Godfrey Fernandes

Reputation: 139

Why does undefined is returned when an object returns a value

Trying to understand why is "undefined" returned when consoling out the output.

  var learnFn = (function(){


   var callMe = function(){
    console.log('hi');
   } 

   return {
     name:"tom",
     callMe: callMe
   }
  })();

  console.log(learnFn.callMe());

  Output:

  "hi"
  undefined

Upvotes: 0

Views: 43

Answers (1)

Quentin
Quentin

Reputation: 944016

The function you are calling:

var callMe = function(){
    console.log('hi');
}

… has no return statement. So it returns undefined (which you then log, after the console.log statement inside that function has run).

Upvotes: 3

Related Questions