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

JavaScript Foundations 'null and undefined' challenge task

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

The code is

var myUndefinedVariable;
var myNullVariable = null;

if(myNullVariable == myUndefinedVariable) {

I've watched the video twice and I just don't get it -- I've tried making both variable = null and using the triple equal (===) with "bummer" results. The part I don't get is the identical(). ???

5 Answers

Dave McFarland
Dave McFarland
Treehouse Teacher

Leah Black

You can use the double equals == in JavaScript to test if two values are equal. But double equals doesn't test true equality -- the JavaScript interpreter converts some values -- like undefined and null -- into slightly different versions. For example if you compare undefined==nulll the JS interpreter treats this as true because it converts both of those values to false. However, the triple equals operator === checks "strict equality" to make sure that the two values aren't just equal but of the same type.

So to really see if undefined is the same as null you use triple equals: undefined===null which turns out to be false.

The identical() part is confusing. It's a function that's being called -- however, Jim didn't put that function anywhere on the page. The function is just there to illustrate that that function would ONLY run if the two values are identical. Confusing, yes. Sorry about that.

Maybe the identical(); part could be replaced with console.log("Values are identical.");.

You are not supposed to change the variables. You just have to modify the if statement so that the identical() function is triggered only when it's supposed to be triggered.

null and undefined values are both falsy, so the equality operator (==) treats them as equal. If you use the identity operator (===) they will not be treated as equal.

The equality operator enables something called type coercion. If it cannot compare values (because they're of different types) it will convert them so that it can compare them.

The identity operator doesn't enable type coercion and as such is faster and more accurate.

Thanks to you both. I was able to complete the challenge but I'm still a bit befuddled by the whole thing!

Prompted by how often this question appears on the forums, I made a lengthy post trying to explain how type coercion happens and what it really is. It might look a bit complicated at first, but it really does explain what happens in the background. Maybe it'll help you.

idan ben yair
idan ben yair
10,288 Points

I still didnt get it, I tried undefind===null in the method, it did not work, I tried giving the value undefind to the variable myUndefinedVariable and that didnt work..

Dave McFarland
Dave McFarland
Treehouse Teacher

Hi idan ben yair

Are you typing undefind? It should be undefined. The first one is missing a second 'e'.

idan ben yair
idan ben yair
10,288 Points

Ohhhhhh lol sorry! I was missing an e.. Thanks!!! I got it resolved :)