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

Jarrod Brooks
Jarrod Brooks
5,071 Points

why is null === undefined ?

I am doing this code challenge and I watched the video several times. I tried doing this in the console several times and I do not understand why null could be === to undefined. It is my understanding that null and undefined share the same type which would be mean that null == undefined is true. However because null and undefined are not the SAME and same TYPE then: null === undefined should be false. What am I missing here?

6 Answers

David Smith
David Smith
21,220 Points

I think the Instructions and the code should be worded differently to avoid confusion like this.

identical() is just a function name, not actually what is identical

Below is an explanation with double and triple equals:

myNullVariable == MyUndefinedVariable will equal true, because type conversion takes place, so the identical method will be called

myNullVariable === myUndefinedVariable will equal false, because null does NOT equal undefined, so the identical method will not called.

So the correct answer is to use the triple equals (===) to not call the identical() function. This is what the instructions are asking you to do

The question is saying that given the original code

 var myUndefinedVariable;
 var myNullVariable = null;

    if(myNullVariable == myUndefinedVariable) {
        identical();
    }

That the identical method IS BEING CALLED but the variables are not identical, fix this. So in effect add the extra (=) t make it (===) therefore preventing the identical() function from being called as they are not equal.

Does that clear it up, it confused me at first but the question is correct. You are getting a pass as the output is false. Try putting all the code into the console

This is the console outputs I get from the two lines you are talking about

null == undefined
true
null === undefined
false

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

David Smith
David Smith
21,220 Points

Both behave the same, however type conversion takes place

Double equals (==) Does type conversion

Triple equals(===) Does NOT do type conversion. It matches the types as they are to do the comparison

A really good website to view a table of equality comparisons is here A more detailed explanation is in a stackover flow question here

Whenever I code JavaScript i always use the Triple equals to avoid subtle bugs from appering

Jarrod Brooks
Jarrod Brooks
5,071 Points

That is what I get also, however when I use that logic I don't get a correct answer. This is the code challenge http://teamtreehouse.com/library/null-and-undefined-2
instructions: The identical() method is being called but the variables are not identical. See if you can fix the code. <script>

    var myUndefinedVariable;
    var myNullVariable = null;

    if(myNullVariable == myUndefinedVariable) {
        identical();
    }

</script>

To get a correct answer it wants me to say: if(myNullVariable === myUndefinedVariable) { identical(); }

However I don't understand why that is a correct answer because that would imply that null === undefined would be true. What am I missing?

The task is saying that the method identical() is being invoked even when the values are not identical (null and undefined are falsey values so through the use of the standard equality operator (==) they're getting converted to boolean false — type coercion, and they appear to be equal), and that shouldn't happen. The task is asking you to prevent that from happening, and you do that by using the strict equality operator (===).

Jarrod Brooks
Jarrod Brooks
5,071 Points

Thank you for your replies. I understand it now. I was confused by the wording in the question. Once I looked at it differently I was able to grasp it.