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 trialJacinda Zhong
3,695 PointsDifference between == and ===?
Hello, What is the difference between
console.log(1 != 3);
and
console.log(1 != =3);
?
Thank you!
Jacinda Zhong
3,695 PointsOh!! Ok, that makes sense. Awesome, thank you!
Adam Sackfield
Courses Plus Student 19,663 PointsYour welcome!
4 Answers
Adam Sackfield
Courses Plus Student 19,663 PointsThe identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.
Colin Stodd
3,160 PointsI like to think of it as:
"=" ~ "equals" or "is" (whatever one you prefer, I like "is").
"==" ~ "equals to".
"===" ~ "is equal to."
as you can see for each "=" sign, there is the same amount of words. (Kind of how I remember things.)
if you throw in the "!" then just put "not" in front of those words.
Adam Sackfield
Courses Plus Student 19,663 PointsNice tip +1 for that
G Spilio
Courses Plus Student 1,413 PointsBriefly, double equals will perform a type conversion when comparing two things; triple equals will do the same comparison without type conversion (by simply always returning false if the types differ); and Object.is will behave the same way as triple equals, but with special handling for NaN and -0 and +0 so that the last two are not said to be the same, while Object.is(NaN, NaN) will be true. (Comparing NaN with NaN ordinarily—i.e., using either double equals or triple equals—evaluates to false, because IEEE 754 says so.)
Do note that the distinction between these all have to do with their handling of primitives; none of them compares whether the parameters are conceptually similar in structure. For any non-primitive objects x and y which have the same structure but are distinct objects themselves, all of the above forms will evaluate to false.
Jacinda Zhong
3,695 PointsThanks G Spilio! That's a great explanation for object.is, I didn't realize there were even more comparisons available, especially for +0 and -0.
G Spilio
Courses Plus Student 1,413 PointsJust to make it clear, you are better off using === instead of Object.is for everything except some edje cases. See link for more details.
G Spilio
Courses Plus Student 1,413 PointsJust to make it clear, you are better off usin === instead of Object.is for everything except some edje cases. See link for more details.
Adam Sackfield
Courses Plus Student 19,663 PointsAdam Sackfield
Courses Plus Student 19,663 PointsThe identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.