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

indentical()

Hello!

this code is expecting for identical variables for the identical () to run. I have set both variable to null and I have used the strict equal operator, but I still can pass Not sure. Cheers.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Variables</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
    <script src="ignore_this.js"></script>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Variables: Null and Undefined</h2>

    <div id="container">
    </div>

    <script>

        var myUndefinedVariable = null;
        var myNullVariable = null;

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

    </script>
  </body>
</html>

7 Answers

In this code challenge supposed that your make code right (code should check variables if they are identical) but not to run identical() function in any way.

So if you use strict equal operator and leave variable myUndefinedVariable undefined you code will pass the challenge.

var myUndefinedVariable;
var myNullVariable = null;

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

Hello Alexander,

I thought a variable that has not been initialized is considered 'undefined', so when comparing it to a variable that is storing 'null', we should use the double equal sign. And, I thought the tripple equal sign only accepts a condition that is exactly the same in type and value. hmm why does this code pass with a tripple equal?

Cheers!!

Hello again orange sky!

You are perfectly right. (undefined == null) returns true and (undefined === null) returns false.

But the task of the Code Challenge is to make the code correct in a way that function identical() should be called only if variables are identical (type and value). So, the goal is to compare variables with strong equality operator even if function identical() isn't called.

I'm sorry, I'm not native English speaker and I can be unclear in my explanations, but I will be glad to continue answering your questions.

Hello Alexander! Your english perfectly fine. I just don't have enough experience with coding. Ok. lets see if I can get this. You said the code below will pass, but to me the word identical means exactly alike in type and value. If (undefined === null) returns false. Can you you please tell me why something that returns false would cause the identical() to run.

Once again, it is not your English,the problem is me, but it should get better, they say practice makes perfect. :)

var myUndefinedVariable; var myNullVariable = null;

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

Can you you please tell me why something that returns false would cause the identical() to run.

Exactly, this function identical() is not called, but we don't need it to pass the challenge.

This is task from code challenge:

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

It sounds ambiguously. And you have made assumption that you need to make variables identical in order to the function identical() is called and the code is fixed.

But challenge's authors had in mind that you need to make the equality operator strong and after that function identical() will stop be called and code will become fixed.

Hello Alexander!! Now, I got the point of the assignment. Thanks a lot for all your help.

***By the way, when we say ( 'undefined' == to null) , they are equal in value (0) and not type, right? because 'undefined' means the variable has not been assigned a value, and null means the element or variable does not exist, right? Just making sure I am totally clear on this point. Cheers!

By the way, when we say ( 'undefined' == to null) , they are equal in value (0) and not type, right? Not exactly. They cannot be equal in value, because they don't have one. But... they are equally don't have value, so ( undefined == null ) returns 'true' :) Anyway, if you are beginner in programming and you know that ( undefined == null ) returns 'true', but ( undefined === null) returns 'false', it's far more than enough.

If you want to be totally clear on this point, I suggest you a good article about JavaScript comparison operators.

MDN is a right place to find documentation about the Web technologies.

Also there is a complete description of The Abstract Equality Comparison Algorithm that uses JS when it's performing not strong equality comparison. It can be difficult to understand at the first attempt, but pay attention to second and third items: 2. If x is null and y is undefined, return true. 3. If x is undefined and y is null, return true.

I hope I've not overwhelmed you with information :)

Thanks Alexander for your thorough response:) I will definetely have a look at that link. Cheers!

You are welcome!

Michelle Cannito
Michelle Cannito
8,992 Points

I think this is a poorly worded Challenge. Adding words as shown in all-caps below would certainly help explain what the task is: The identical() method is being called but SHOULD NOT BE CALLED BECAUSE the variables are not STRICTLY identical. See if you can fix the code SO THAT THE IDENTICAL() METHOD DOES NOT RUN.

I totally agree.

Me too!!!!