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

Pablo Motta
Pablo Motta
6,644 Points

I'm having a really hard time figuring this problem out. Could anybody help/

I can't figure out how to fix this code though what I just learned in the previous lesson. How can I fix this:

    var myUndefinedVariable;
    var myNullVariable = null;

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

?

5 Answers

Kevin Korte
Kevin Korte
28,148 Points

Where I was wrong is that null == undefined will return true, and null === undefined will return false. The double equals does the comparison after any necessary type conversions, the triple equal will not do any type conversion at all.

My mistake was I had forgotten/didn't realize that after a type comparison null and undefined evaluate the same. I was able to confirm this with this simple codepen. Just change the double equals to a triple equals and back. When the box turns green, the if statement evaluated to true.

http://codepen.io/kevink/pen/EupGd

So now it begs the question, what does identical(); set up to do?

Kate Hoferkamp
Kate Hoferkamp
5,205 Points

The world may never know! lol

Kevin Korte
Kevin Korte
28,148 Points

Your if statement isn't true, so nothing runs. <!--EDIT This is actually not true. My mistake -->

A property that is not defined is set to undefined by default. Your second variable is set to null.

So really your if statement is saying

if( null == undefined ) { identical() };

Pablo Motta
Pablo Motta
6,644 Points

Thanks Kevin!

I made the changes from:

var myUndefinedVariable; var myNullVariable = null;

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

to::

var myUndefinedVariable; var myNullVariable = null;

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

I'm assuming "==" does't mean identical and "===" does right?

Kevin Korte
Kevin Korte
28,148 Points

I made a mistake, let me retry in a new post.

Kate Hoferkamp
Kate Hoferkamp
5,205 Points
 var myUndefinedVariable;
    var myNullVariable = null;

    if(myNullVariable === myUndefinedVariable) {
        identical();
    }
Pablo Motta
Pablo Motta
6,644 Points

Thanks Kevin and Kate, and yes, identical(); was a big source of confusion to me.