0

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
5
  • 1
    Because cosole.log() returns undefined. If you run this in the dev tools, you'd see exactly that. Commented Oct 23, 2016 at 16:15
  • @vlaz: That's not the reason. In the code above, it wouldn't matter what console.log() returns. Commented Oct 23, 2016 at 16:21
  • 1
    @MichaelGeary true, I didn't pay much attention to what the function was - I thought somebody was executing some code in the console and confused about undefined coming from console.log. Happens regularly enough to have fooled me. Commented Oct 23, 2016 at 16:24
  • @vlaz yes i was confused about undefined coming from console.log . Now got it, but how to get rid of it? Commented Oct 23, 2016 at 16:28
  • 1
    @godfreyfernandes don't log a function call that returns undefined or alternatively, do not return undefined from the function call you log the result of. Commented Oct 23, 2016 at 16:30

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.