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 trialShannon Yeh
8,987 PointsCode Challenge Null and Undefined Help...
I am having trouble "fixing this code"
The identical()
method is being called but the variables are not identical. See if you can fix the code.
var myUndefinedVariable; var myNullVariable = null; if(myNullVariable == myUndefinedVariable) { identical(); }
really not sure what to do.
10 Answers
Jenny Shi
10,015 PointsThis question could be worded better, but I think what it's saying is that currently (before you add any code) ...
the identical()
function is being called. This is because myNullVariable
and myUndefinedVariable
are equal when compared with the ==
operator.
However, as the question is stated, the variables are not identical in the strict equality sense, and therefore identical()
should not run.
The correct solution would be to replace ==
with ===
and make the if-statement false. Hope this helps!
Shannon Yeh
8,987 Pointsnvm figured it out! it was adding three ===
Luqman Shah
188 Pointslol great job!
Steven Wade
6,425 PointsSorry, but this doesn't make sense to me. From my understanding, if statements execute only if the statement is true.
If myNullVariable == myUndefinedVariable
then that should come out as true as opposed to myNullVariable === myUndefinedVariable
would come out as false (as the video lesson states).
Alexandra Silcox
24,486 Points@Steven that is how I understood it as well. It was even demonstrated in the lesson when. === is a strict operator and null and undefined are strictly not equal (they are two different types), only conceptually they are equal.
undefined == null true undefined === null false
Can someone else please explain this? I'm very confused.
Colin Lewis
9,021 PointsYes, Please, I really don't get it. I feel like it should have been the other way around.
David Taber
Courses Plus Student 3,574 PointsThis is a poorly worded question. New to JavaScript but not to programming. When I first looked at this--the first thing that jumps out is that there is no method definition for identical(); I thought that this was java specific and maybe you could just call a non existent method and get no error so I typed it into the Chrome interpreter and got the following error.
ReferenceError: identical is not defined.
The second part of the ambiguity is this. Without the identical() method being defined is the question implying that the code should be like...
var identical = function(){ var myUndefinedVariable; var myNullVariable = null; if(myNullVariable === myUndefinedVariable) { return true; } }
identical();
thirdly why doesn't the "code checker" in the code challenge flag the error or am I missing something?
Michael Kalmykov
8,919 Pointsalso confused.
== means sorta kinda the same
=== means ABSOLUTELY without a doubt eqaul
soooOOOoooo if
html myNullVariable == myUndefinedVariable
Is not sort of the same then
html myNullVariable === myUndefinedVariable
should be ABSOLUTELY "strictly" not equals.
*confused face
James Barnett
39,199 PointsI like your description of equality (==) and identity (===)
Leyla Movahedi
6,549 PointsI think all they are asking here is to correct the code for identical method, which is the triple equal sign. ===
The original code has the equal method. ==
Evan Hoefling
Courses Plus Student 5,235 Pointswhy are they identical???
evansimoni
4,542 PointsI too would appreciate an explanation here.
Gergely Marton
27,180 Pointsall you need to do is add an = to the html myNullVariable == myUndefinedVariable line so it should look and work like this html myNullVariable === myUndefinedVariable
Evan Hoefling
Courses Plus Student 5,235 PointsI think what the code challenge is trying to say is that since they are not identical, we shouldn't execute the identical function. So we want to set the condition to false. This is just a guess, so don't quote me on it.
Adrien Beaulieu
8,127 PointsYes, puzzled for some time but here is what I can catch from this exercise.
1- We declare 2 variables that are clearly expressed as one which is Undefined and the other one which is Null. It's the premise. var myUndefinedVariable; var myNullVariable = null;
2-We need the loop to be run (if not, the exercise won't make any sense).
if(myNullVariable == myUndefinedVariable) {
identical();
}
3-So in order for identical to be executed (so to write) the IF must be true so the THEN could be performed.
Solution : the only one is to === them.
Hope I got it right.
Zach Allan
19,452 PointsZach Allan
19,452 PointsThis is the best explanation of the question. I fully understand it now. Thank you!