What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?
2 Answers
Take a look here: http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html
The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.
0 == false // true
0 === false // false, because they are of a different type
1 == "1" // true, automatic type conversion for value only
1 === "1" // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
18 Comments
Koen Zomers
Thanks for the clear answer! I guess if compared to C# the == would also be == and === would translate to .Equals()
hrishikeshp19
what about "new String()===new String()", both values and types are same. But statement returns false.
l8nite
@hrishikeshp19: in that case, the values are actually different (different object references)
Earth Engine
@KoenZomers I don't think your C# case is right. Actually there are no equivalents in C#. == in C# do a reference compare, and Equals do predefined compare, none of them have equivalents in JavaScript either.
danorton
@hrishikeshp19,
new String() is not of a string type, it's of an object type, so the === rule for objects applies. Usage of primitive strings, however, often results in coercing the strings into String objects, so the difference is subtle. If you were to assign new String() to two different objects, s1 and s2, the valueOf() method on each would return a string primitive for each, and s1.valueOf() === s2.valueOf() would return true. |
=== and !== are strict comparison operators:
JavaScript has both strict and type-converting equality comparison. For
strictequality the objects being compared must have the same type and:
- Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
- Two numbers are strictly equal when they are numerically equal (have the same number value).
NaNis not equal to anything, includingNaN. Positive and negative zeros are equal to one another.- Two Boolean operands are strictly equal if both are true or both are false.
- Two objects are strictly equal if they refer to the same
Object.NullandUndefinedtypes are==(but not===). [I.e. (Null==Undefined) istruebut (Null===Undefined) isfalse]
5 Comments
Filip Vondrášek
So, if I do for example:
if (input == null) ..., will it also make the condition true when input is undefined?Matt Browne
The above makes it sound as though a == comparison wouldn't check all the things in the first bullet point, "the same sequence of characters, same length, and same characters in corresponding positions" but in fact it does. As far as I can tell the only real difference when comparing two strings is that with ===,
new String()===new String() returns false (different object references). But new String should be avoided anyway.CodyBugstein
-1 The question was "what is the difference?" and you only explained the strict operators, but not the difference between them and the non-strict ones
T J
I didn't exactly get "Two objects are strictly equal if they refer to the same Object" - what? by two objects, does it mean two reference variables..?
Luis Perez
For plain English description of the issue see stackoverflow.com/a/38856418/984780
==is===with type converting (aka coercion). To really understand what I mean you can look at this JavaScript function that behaves exactly like==: stackoverflow.com/a/38856418/984780==.==), also known as the if-same-type-then-strict-equality-comparison-otherwise-treat-null-and-undefined-and-document-dot-all-as-equal-but-if-string-involved-with-number-or-bigint-then-coerce-string-to-respective-numeric-type-but-if-boolean-involved-then-coerce-it-to-number-but-if-object-involved-then-coerce-it-to-primitive-and-if-numeric-types-involved-then-compare-their-numeric-values-with-distinct-infinities-and-nans-being-unequal-and-then-repeat-as-needed operator.