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
Jeremy Burghardt
4,037 PointsNot sure I understand how this challenge works
So I just did an early challenge in the 'Variable' section of the 'Javascript Foundations' course, and I'm not sure I understand how it works.
This is the question:
"The identical() method is being called but the variables are not identical. See if you can fix the code."
Here is the code:
var myUndefinedVariable;
var myNullVariable = null;
if (myNullVariable == myUndefinedVariable) {
identical();
}
In the video beforehand I was given the impression that == doesn't need to be exact in order to evaluate to true. Since one variable is null, and the other variable is undefined, would the == see them as being similar enough to be true?
Also, I gave myUndefinedVariable a value of null, so it would exactly match that of myNullVariable, but that didn't work. Why? If both variables are exactly the same value, wouldn't == evaluate as true?
The solution ended up being to just make the == in the 'if' statement a === instead. Why is that? The impression I got from the prior video made it seem as though === means it needs to be exactly the same to evaluate to true, and yet one variable was undefined, while the other was null.
I don't get it. It seems simple enough, but this challenge just confused me. Did I miss something? Did I not understand how these things work?
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Jeremy,
As the code is written initially, the identical() function is being called because the if condition is true. However, the identical function should not be called because those 2 variables are not identical.
What it's trying to tell you is that the condition is wrong. It's currently evaluating to true but it needs to evaluate to false so that the identical() function doesn't execute.
That's why you have to change it to ===. Now, the identical() function will only be executed if the variables match both value and type.
Chris Shaw
26,676 PointsHi Jeremy,
The triple === equals does mean that both sides of the operator must have the same type and value, in JavaScript null and undefined using just an double == equals works because they both evaluate to falsy values so when compared the statement is true, however using the triple equals causes the statement to become false as undefined is not the same type as null even though they still both evaluate to false.
Below is an example that should make this a little bit more clear.
if ('1' === 1) // This is false as a string value of 1 is not the same as the integer 1
Let me know if it's still confusing.
Jeremy Burghardt
4,037 PointsThanks! This reinforces my knowledge that I did understand it correctly, so my question is, what was going on with that challenge? It said the variable weren't identical, and then asked me to fix it, but the solution was to change the == to ===.
Since undefined === null evaluates to false, why was that the answer to the challenge? It's just strange because the video (and your explanation) explain it correctly, but the challenge seems wrong.
Chris Shaw
26,676 PointsI think it's the context of the question that makes it confusing.
The identical() method is being called but the variables are not identical. See if you can fix the code
From the teachers point of view this would have made perfect sense and I won't lie, it does to me as well but for newbies I can see how it would throw you off a little as it sounds like both undefined and null don't equal falsy values but instead the question is implying that their types aren't identical.
I think if the question was worded as the below it would make more sense but I don't think the teacher would want it given away too easily what needs to be done.
The identical() method is being called but the variables are not of the same type. See if you can fix the code
Jeremy Burghardt
4,037 PointsJeremy Burghardt
4,037 PointsOh! I get it. I was thinking that the identical() function was supposed to evaluate to true, so when I saw it was already correct, I got confused. Didn't realize it wanted it to evaluate to false. I get why it wanted it done this way now, I just think the intent of the challenge may have been explained a little better.
Thanks!