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

JavaScript JavaScript Foundations Numbers Comparisons

Difference between == and ===?

Hello, What is the difference between

console.log(1 != 3);

and

console.log(1 != =3);

?

Thank you!

The 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.

Oh!! Ok, that makes sense. Awesome, thank you!

Your welcome!

4 Answers

The 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
Colin Stodd
3,160 Points

I 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.

Nice tip +1 for that

Sameness in Javascript

Briefly, 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.

Thanks 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.

Just to make it clear, you are better off using === instead of Object.is for everything except some edje cases. See link for more details.

Just to make it clear, you are better off usin === instead of Object.is for everything except some edje cases. See link for more details.