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

Variables Null vs. Undefined

I am currently on the objective for the Null Vs Undefined section of Javascript Variables, and though I was able to pass the objective, I am a little hesitant to move forward.

The objective states "The identical() method is being called but the variables are not identical. See if you can fix the code." This to me says that we want the function identical() to be called, but it isn't because the variables are not equal. The code given declares two variables and compares them using the == operator, as shown below.

var myUndefinedVariable; var myNullVariable = null;

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

Maybe I am just misunderstanding the given objective, but looking at the default provided code for this exercise, it seems that identical() would run because the two variables are compared with ==. Using the === operator completes the challenge, but in my head it would mean that the if statement would come out as false because null isn't strictly equal to undefined, meaning identical() wouldn't run. Am I missing something? Is the objective to stop identical() from running?

2 Answers

Christopher Hall
Christopher Hall
9,052 Points

You're right, the objective is a little vague. However I think in this case we don't want the identical method to run if the variables are not actually identical. Like you said, the identical() function would not ever run in this case with a null and undefined variable compared with ===.

This is a good example of why Javascript is considered a loosely typed language, compared to strongly typed languages like Java or Objective C.

You are correct about how it illustrates loosely typed vs. strongly typed. After watching some Objective-C videos this is very apparent. It's also good to hear that I'm not the only one feeling the ambiguity of this task. Thanks Christopher!

Christopher Hall
Christopher Hall
9,052 Points

Adrian, you might want to take a look at this article which goes into more detail about truthy and falsy values in Javascript.