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 trialJames Davis
Courses Plus Student 2,151 PointsJavaScript Foundations: Variables - Null
Hello. I am doing the JavaScript Foundations stage and I am confused on one part. When comparing (myVar === undefined) Jim gets a false. When I do the same script, I get true. I am confused.
I thought that the === is Strict Equality therefore it evaluates both the data type and the value. Since myVar was declared but not assigned a variable it is undefined both by data type and variable. So it would be true right? Or am I confused?
I hope I am explaining this clearly.
9 Answers
Jim Hoskins
Treehouse Guest TeacherHi James!
You are right. Technically, in the language spec, undefined is a variable that can be assigned to (undefined = true), causing confusion. The point I was making is that (x === undefined) is an unreliable way to test for undefined because bad code may have reassigned the value, instead use (typeof x === "undefined").
This point is illustrated by the little-known fact that in FF you cannot assign to the value undefined, whereas you can in other browsers.
The moral of the story is dont test via (x === undefined), use (typeof x === "undefined") instead.
- edit - I just tested in my latest chrome build and discovered assigning to undefined is also being prevented, which was not always the case. Again, just dont compare to the default undefined, it's too confusing. Use typeof to be sure.
Liban Shire
Courses Plus Student 4,147 PointsWell it is true and you're right, I'm not sure why Jim got it fase you should go over the video again and see if that's correct.
another thing he did I noticed is that he set
undefined = true;
and then he did
console.log(myVar === undefined); and this unfortunately false.
James Davis
Courses Plus Student 2,151 PointsBelow is the code I have from that lesson. Maybe I fat fingered something and I am missing it. But my code looks exactly like his and I still get true true instead of true false. I am frustrated because I thought I understood this but it seems I don't.
/**
- JavaScript Foundations: Variables */
var myVar;
undefined = true;
console.log(typeof myVar === "undefined"); console.log(myVar === undefined);
Liban Shire
Courses Plus Student 4,147 PointsYou should get true for the first one and false for the second one. as long as your answer is what the Jim is saying that's all that matters.
James Davis
Courses Plus Student 2,151 PointsThat is the problem. In Firefox, I am getting true true. Not true false.
Liban Shire
Courses Plus Student 4,147 Pointswhat about in chrome?
James Davis
Courses Plus Student 2,151 PointsIn my work environment I only have IE6 and FF10 as my browser options.
Elaine Ryan
6,771 PointsI'm having the same issue as @jamesdavis. I get true true with this code:
var myVar;
undefined = true;
console.log (typeof myVar === "undefined"); console.log (myVar === undefined);
I get Jim Hoskins point, that we should use typeof myVar === "undefined", but I'd like to know why my console says true true.
Any ideas?
Kalen Loncar
8,743 PointsJim Hoskins So the reason the code came up as true true, is because Chrome prevented assigning to undefined? Why isn't there an error message displayed?
James Barnett
39,199 PointsJames Barnett
39,199 Points>
I just tested in my latest chrome build and discovered assigning to undefined is also being prevented, which was not always the case.From http://www.2ality.com/2013/04/check-undefined.html
undefined
is a property of the global object (and thus a global variable). Under ECMAScript 3, you could change its value. Under ECMAScript 5, you canβt do that, any more.