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

huckleberry
huckleberry
14,636 Points

Task Objective: identical() method not covered and the solution just doesn't make sense to me.

I notice that in the challenge we're given a task that involves changing the == to === in order to make things work.

This didn't make sense to me. If we change == to === that makes it false and the statement doesn't run. The task said that 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>

right now in the starting code the condition evaluates to true because of the double equal operator and how it treats a null - undefined comparison. So it was logical to ME that in order to make them identical that you'd have to change either the first variable to null as its value or remove the assignment operator and the value of null from the 2nd variable in order to make THAT undefined as well.

So what gives? How does changing == to === make anything work here? Doesn't that make the condition evaluate to false?? And what's going on with the identical operator?? Does the presence of that affect the operation of everything and make it crazy backwards?? If so, why would you include that in the challenge when you didn't cover it?

I even ran the code pre-fix in my practice pages on chrome to see what is going on and all it does is return an Uncaught ReferenceError: identical is not defined. But when I change the == to === there's no console error. So please, someone explain this mess lol.

Thank you!

3 Answers

Hey huckleberry,

You know, I just remember flying through that challenge without giving it any thought, but I'm glad you did. The challenge itself is actually wrong, and I can prove it. If you go into your browser console, and copy and paste the variable declaration of var myUndefinedVariable; var myNullVariable = null; and then run some logic tests on them, you can see that the original way that challenge is presented is the correct way and that what you have to change it to is incorrect.

For example, running myUndefinedVariable == myNullVariable returns true because in a sense, undefined and null are the same. But the strict comparions of myUndefinedVariable === myNullVariable returns false because they are not the exact same.

Honestly, I find that course extremely wonky, and I don't know why it's still a part of the Front End Development Track. I haven't yet taken Dave McFarland's JavaScript Basics but I've heard really good things about it. And I like his other courses, as well.

huckleberry
huckleberry
14,636 Points

Precisely!! & Thanks for taking the time to reply.

They explained right in the video that because both null and undefined are falsey values that the interpreter will sort of adjust the value and treat them as being the same when using the non-strict double equal operator of ==

So right off the bat I'm looking at this and going, ok, well, it already evaluates to true and they didn't cover this identical() method at all soooo ... maybe identical() is special and will cause the == to evaluate to false?? So I tried all the options to make the values actually idenetical (changing the null var to undefined, giving the undefined var a value of null, using ===, etc..) nothing worked until leaving them in their original state and applying the === which, as you pointed out, returns false. So I was left assuming that the identical() method does something else. Like, maybe it takes a statement that returns false and converts whatever values you were comparing into identical values? I dunno but like you said... wonky lol.

In regard to the ongoing battle of Basics v. Foundations, I will say that I did take the basics course and found it to be very well laid out, thorough and informative and I'm glad I did it before the foundations. But as you know it doesn't include various things that are in this course so naturally I'm now on to this one.

But, I did actually take intro to programming first before going to the Basics course which I see many people have initially done only to struggle endlessly with. I will admit, the Intro to Programming course was "intensive" rather than "guiding" as opposed to the Basics course, but I found it rather enjoyable and I always take a copious amount of notes, create my own examples based on what was gone over and consistently pause and rewind while doing my own google-fu on the topic to see what else can be done with it and see examples of realistic applications of the concept at hand. I don't simply watch the vid and go "cool, I know loops now... next" which I think is an ill advised approach that many seem to take on here.

Either way, I think both methods have their merit. One is clear and concise and leads you by the hand a la McFarland and the other, while more intensive and further reaching in its scope, leaves the user with some responsibility to explore the concept in a more rigorous self-driven fashion which is a perfectly well suited approach in my opinion considering the field in which we've endeavored to master. It certainly is one heck of an autodidactic rabbit whole we've all chosen to venture down and I think the experience of having core concepts covered for us with a good explanation but still needing additional smoothing out and exploration on our part is good experience for us all and, in the end, will help us to not only learn but internalize the language better.

Thanks again for your reply and verifying what I was thinking.

Cheers!

All I can say is I'm glad I'm not the only one frustrated by this challenge. The wording itself is very confusing:

The identical() method is being called but the variables are not identical. See if you can fix the code.

Fix the code to do what? What is the identical() method?

I agree. I also had trouble with it. Wish they had hints since we can't read their minds!