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

If you declare a new variable without a value, what is the "automatic" value being set?

I was almost going nuts on the last challenge!

I had everything correct but was getting "NaN" on the last function. I figured I should at least try to find the answer myself before getting it from your "solution video".

After hours of searching, and trying out different things (like parseFloat) that did not work, I finally gave up and watch your solution video.

I was befuddled when I saw that my code was exactly like yours! Well, almost.

One small change was needed.

When I declared the variable inventory_Value in my code I did like this: "var inventory_Value;"

I noticed that in the video you guys did like this: "var inventory_Value = 0;"

I tried that thinking it couldn't be that easy... But that did the trick for me!

Putting this out there if anyone else struggles with the same thing.

Thanks TTH! And good luck everyone else :)

1 Answer

Steven Parker
Steven Parker
229,732 Points

If you just declare a variable, but do not assign it, then it's value is still "undefined". Any time you do math with "undefined" the result will be "NaN".

That makes sense, thanks Steven!