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 Variables Null and Undefined

i wish there was some context given in this lesson

i am trying to do the js foundations course and i am having a lot of trouble following along with the instructor. in a nutshell it just seems like the things he is covering and explaining have no practical use for a newbie web developer trying to learn javascript for the first time.

forgive me if this seems stupid as its quite possible the things he is explaining are core concepts used everyday by js developers - however if this is the case i think it would be very helpful for me to explain a practical example or use case for whatever concept or lesson is being taught before explaining them.

by way of a single example that was just covered, when would someone need to test whether a variable is in fact undefined? or when would someone need to test whether undefined is in fact undefined and hasnt been changed elsewhere in the code?

Chris Shaffer
Chris Shaffer
12,030 Points

It's just my two cents, but I found that JS seems very abstract and hard to wrap my head around until I went through the JQuery lessons over at Codecademy.

JQuery is a JS library, and as such the main concepts of JS apply. I went through that course, then I went to Try.JQuery.com which is created by Code School and linked to off of the actual JQuery API home page (so even the creators and maintainers of JQuery think its good :)).

I'd strongly suggest just running through the JQuery lesson at Codecademy. You could probably finish it in a day or two, but I took my time and did an hour a day for about 5 days. Still, not long and it will make JS make a lot more sense.

edit I should also mention that as far as practical applications of JS for a web dev, JQuery has a lot more "instant gratification" built-in and far more practical uses.

You'll find yourself using JS in an "as needed" capacity when working with JS libraries; you'll likely not be writing massive amounts of JavaScript when you start web development. That's what APIs are for, these days.

1 Answer

Stone Preston
Stone Preston
42,016 Points

because doing things with an undefined variable will lead to problems.

suppose you had a variable named user that holds the value of a user object. now suppose you wanted to do something with that user variable. if for some reason that user was null or undefined (which could happen if say the user account was deleted or something) and you ran code that expected that user variable to have a value you would get an error. You dont want errors, which is why you use an if statement to defend your application against errors (which is why this concept is known as defensive programming.)

if (user != NULL) {

//user exists, safe to do stuff with it

} else {

 //user does not exist do something else
}

the degree to which you apply this concept depends on the situation (it can be a bit much to try and defend against any possible error occurring). If you know that variable will always have a value then you really dont need to test if its null. never hurts though