Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Functions Anonymous Functions

Undefined

Hello!

when I just ran this code below, (and I didn't type anything into the webconsole), however I got;

"hellojim"

undefined

1) It makes sense for it to return ''hellojim" , but in this case, what does 'undefined' mean (again, I just ran this code, I did not type anything into web console: jim.green().

2) When I call in the console: jim.green(jim); I get undefined and then on the second line, I get: "hellojim" My jim object has console.log( 'hello' + this.name); so the 'this' should bind to the object jim and return its name,so what does 'undefined' right before it print "hellojim" mean? I also get the same output when I type in the console: jim.green('jim');

<!DOCTYPE html>
 <html>

 <head>  
  </head>
  <body>


    <script>
    var jim = 
    { 'name' : 'jim',
    'skills': ['js', 'excel'],
    'favorite color' : 'green',
    green : function(){
    console.log( 'hello' + this.name);

    }


    };
    console.log(jim.green());           

    </script>
  </body>
</html>

2 Answers

J Scott Erickson
J Scott Erickson
11,883 Points

I can't recall the order of the course, but undefined will be explained as the return value of the method that you called to log 'hellojim' to the console. Every method has a return value, and if it is not defined explicitly a method will return undefined.

An addition to my original answer, when you call console.log(jim.greet()); the first undefined is the console logging the return value from the greet() method, which is undefined since it's not specifically defined. The hellojim that prints afterward is the console.log() statement in the greet method in the object.

Thank Scott, the second paragraph makes everything clear to me. :)