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 trialSimon s
Courses Plus Student 2,365 PointsCompletely stuck on what to do to correct the code?
Any help on what to do the question asks 'The identical() method is being called but the variables are not identical. See if you can fix the code' any help will be appreciated.
<!DOCTYPE html>
<html lang="en">
<head>
<title> JavaScript Foundations: Variables</title>
<style>
html {
background: #FAFAFA;
font-family: sans-serif;
}
</style>
<script src="ignore_this.js"></script>
</head>
<body>
<h1>JavaScript Foundations</h1>
<h2>Variables: Null and Undefined</h2>
<div id="container">
</div>
<script>
var myUndefinedVariable;
var myNullVariable = null;
if(myNullVariable == myUndefinedVariable) {
identical();
}
</script>
</body>
</html>
1 Answer
Jessica Barnett
8,028 PointsThink I have an answer for you!
You're using the double equals, but you definitely need to use the triple. That should solve your problem!
if (myNullVariable === myUndefinedVariable)
The difference between the == and the === is that the == tries to convert the two things into the same type first. So if you compare 5 == "5"
, you'll get true, even though one is a string and the other a number.
The === does not change the type, and only returns true if the two sides are actually the same, both in value and type. So 5 === "5"
will be false, but 5 === 5
will be true.
== can work for values like numbers and strings because it's not hard to convert a "5" into a 5. But null
and undefined
are trickier. What javascript ends up doing is converting them both into the boolean value false
so undefined == null
is the practically the same as comparing false == false. This is why you're getting true when you don't want to.
To be honest, I almost never use the double equals. If you're comparing two things, you probably want them to be the same type anyway, so you might as well.
I hope that helps!!!