Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

JavaScript 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
STAFF
Jim Hoskins
Treehouse Guest Teacher

Hi 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.
James Barnett
James 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.

Liban Shire
PLUS
Liban Shire
Courses Plus Student 4,147 Points

Well 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.

Below 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
PLUS
Liban Shire
Courses Plus Student 4,147 Points

You 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.

That is the problem. In Firefox, I am getting true true. Not true false.

In my work environment I only have IE6 and FF10 as my browser options.

I'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
Kalen Loncar
8,743 Points

Jim 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?