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

Either question is wrong or I'm misinterpreting this question

We're trying to function indentical() to work right? If that's the case we need the statement to evaluate to true, and that won't happen unless we use == instead of ===, isn't it? Because myUndefinedVariable == myNullVariable evaluates to true but this isnt the case when using the strict comparator? or maybe I'm not really getting the lesson... Like this,

var myUndefinedVariable;
        var myNullVariable = null;

        if(myNullVariable === myUndefinedVariable) {
            console.log(true); /*<--This is where identical() would be*/
        }
else{console.log("false")}

>false

Which means for the lesson the answer is NOT use the strict equality comparator, as identical() will only run if myNullVariable ==myUndefinedVariable and NOT if myNullVariable === myUndefinedVariable.

Btw I learnt a little javascript from codecademy but they never really got in depth like this so i'm not familiar with the undefined and null difference... Thanks...

Sorry no idea how to format this properly in code blocks...

2 Answers

James Barnett
James Barnett
39,199 Points

The important thing to understand here is the difference between == (equality) and === (identity).

Equality checks if 2 values are equivalent whereas identity checks if 2 values are identical. null and undefined are not identical. However, they are equivalent as they are both falsey values. Much like "1" and 1 are equivalent but not identical.

Equality determines if 2 values are equivalent using type coercion.

That totally cleared it up. I was just really having trouble understanding the identity and equality issue! Thank you both so much!

The point is to invoke identical() if the two variables are the same, but a null value and an undefined value are not truly equal in JavaScript. They are falsey values, so with non-strict comparisons, they're coerced into false and as such appear to be equal.

In the case of this task, identical() is being called even when it shouldn't be, as is the case for the two variables in the task.

The only thing this task wants you to do is to remove the standard equality operator and use the strict one instead.