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

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,135 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,135 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,135 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.