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 Basics Hello, JavaScript! Your First JavaScript Program

When I run console.log in my browser I get message undefined. Is something off with my code? The other code works fine.

Here. is my code:

alert("Hello. Thanks for visiting!"); console.log("Hello from the console."); document.write("<h1>Welcome to my web page.</h1>");

5 Answers

Steven Parker
Steven Parker
231,275 Points

This code looks OK, and it runs fine for me in the browser. If you are running it by typing into the browser console, it's normal for the console to show the return value of any function or expression you type in. Functions that don't return any value (like "console.log") will display "undefined" for the return value. That's normal.

If that's not it, and you're still having trouble, make a snapshot of your workspace and post the link to it here. Then we can replicate the issue exactly.

ibrahim z
ibrahim z
1,710 Points

Hi Lindsay, I know this is late but in case someone else looks: I had the same issue and it was because I had turned off user messages in Chrome. Once that's turned back on it displays properly.

Hi there! How did you turn off user messages in Chrome?

Edit: I figured it out. A quick Google search led me here: "Sounds like you've either hidden JavaScript logs or specified that you only want to see Errors or Warnings. Open Chrome's Developer Tools and go to the Console tab. At the bottom you want to ensure that JavaScript is ticked and also ensure that you have "All", "Logs" or "Debug" selected."

I realized that I was only viewing "Errors Only". So, I clicked to view "Default levels".

I'm running through these classes in Firefox, but on there, in the console menu at the top right is a button called 'Logs'. Until I pressed it, I couldn't see what I had logged to the console using the console.log command either. Perhaps that option needs to be selected on your console window in whatever browser you are using?

Lindsay, So the console.log("Hello from the console") line of your code has already executed as soon as you dimiss the alert dialog. If you're trying to run the console.log() command in the console after this then you'll have to pass it something, if you aren't passing it anything you'll get the 'undefined'.

Actually, you'll get the "undefined" anyway, regardless of what you pass. As Steven mentioned in his answer to Lindsay, functions in JavaScript might or might not return a value- in the case of the log() function, it is defined to not return a value so you'll get that "undefined". Not something you want to dig much into here- the "why" and "how" that value is returned by log() is a more advanced concept than the current lesson.

A big shout out to Mary Barba for the link! I had the same problem. It turns out I had something typed in the >>> console filter <<<, and it was preventing me from seeing "Hello from the console" in the console.