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 trialVitor Oliveira
6,766 PointsDifference between == and ===
1) What's the difference between == and ===?
Could you show me two examples using if statement?
2) Why is undefined === null returns false and undefined == null returns true?
4 Answers
Jeff Jacobson-Swartfager
15,419 PointsThe difference is in how strict they are in comparing the equality of the values. MDN has a good reference on this.
==
is less strict than ===
. A good example of this is 1
vs "1"
:
1 == "1"
> true
1 === "1"
> false
1
is a number. "1"
is a string. In the first comparison, it evaluates to true
because it converts both values to the same type before doing the comparison. The second comparison is comparing the value type as well; since they are not the same type, they are not the same.
Vitor Oliveira
6,766 PointsThank you! It was very helpful.
Jeff Jacobson-Swartfager
15,419 PointsI'm glad I could help!
Dean McKenzie
11,149 PointsThanks Jeff nicely explained!
Ben H
1,114 Points== tests that the values are the same === tests that the values AND types are the same
Jeff Jacobson-Swartfager
15,419 PointsJeff Jacobson-Swartfager
15,419 PointsSimilarly,
undefined == null
returnstrue
because the comparison converts the two values to the same type first.undefined === null
returnsfalse
because the comparison compares the value types, which are different.MDN on undefined. MDN on null.