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
abimanyusaridjo
Courses Plus Student 951 Points"undefined" under h1 tag?
Hi guys,
I'm not sure why I'm getting undefined under my h1 tag...
This is in regards to this video - https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/tracking-data-using-objects/the-build-an-object-challenge-part-2-solution
My code can be found here - https://jsfiddle.net/efqbg4n1/
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! This line is the line causing all your problems:
var message;
When you declare a variable without giving it an initial value, the type is undefined. That's perfectly ok until you start trying to manipulate it with an arithmetic or concatenation operator such as +. So when you start your for loop it is still undefined, but then you immediately try to concatenate to it. This will result in undefined being in the first part of your ending string. The solution:
var message = "";
If you know you have a variable which is going to contain a string later, it's always a great idea to initialize it to an empty string. This prevents this sort of thing from happening if you later want to concatenate to it.
Hope this helps!
abimanyusaridjo
Courses Plus Student 951 Pointsabimanyusaridjo
Courses Plus Student 951 Pointsyoure the best! thank you!!
Andrew Hickman
Full Stack JavaScript Techdegree Student 10,013 PointsAndrew Hickman
Full Stack JavaScript Techdegree Student 10,013 PointsGreat answer. This is something that needs to seriously be hammered into my head.