Difference between == and === in JavaScript [duplicate]
Asked Answered
W

2

1711

What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?

Weinberg answered 7/2, 2009 at 11:53 Comment(3)
== is === with type converting (aka coercion). To really understand what I mean you can look at this JavaScript function that behaves exactly like ==: https://mcmap.net/q/20973/-which-equals-operator-vs-should-be-used-in-javascript-comparisonsHeathheathberry
Some possibly surprising examples of ==.Carner
Ah, yes, the abstract equality operator (==), 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.Plotinus
M
1211

=== and !== are strict comparison operators:

JavaScript has both strict and type-converting equality comparison. For strict equality 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). NaN is not equal to anything, including NaN. 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.
  • Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]

Comparison Operators - MDC

Murat answered 7/2, 2009 at 11:55 Comment(5)
So, if I do for example: if (input == null) ..., will it also make the condition true when input is undefined?Yoakum
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.Anethole
-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 onesInfernal
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..?Rejoice
For plain English description of the issue see https://mcmap.net/q/20973/-which-equals-operator-vs-should-be-used-in-javascript-comparisonsHeathheathberry
J
2037

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
Jainism answered 7/2, 2009 at 11:57 Comment(18)
Thanks for the clear answer! I guess if compared to C# the == would also be == and === would translate to .Equals()Protract
what about "new String()===new String()", both values and types are same. But statement returns false.Immortality
@hrishikeshp19: in that case, the values are actually different (different object references)Ranit
@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.Borszcz
@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.Clive
Also '0' == false // true and null == undefined //trueRecapitulation
Can someone explain why true == "true" is false?Brendin
I think in some cases you would would want to use == over === for example, getting a value from a text input would be '1234'// string not 1234// numberChestnut
Is there a speed difference between == -1 and === -1 for anArray.indexOf(el)?Ridge
@AlexandruSeverin since true == 'true' === false but true == '1' === true, I would say that true gets converted to 1, which is then converted to '1', then '1' == 'true' === false && '1' == '1' === trueRidge
@Elisabeth Just wanted to bump you to see my previous comment on true == 'true' === false. Not an explanation, but it might give you some hints.Ridge
@Jainism you may have a typo above the example box. you wrote: " -The 3 equal signs mean equality without type coercion. - Using the triple equals, the values must be equal in type as well." i think you meant to write "The 2 equal signs mean "equality without type coercion". not "the 3..." Is this accurate ?Royalroyalist
For more unexpected examples check out https://mcmap.net/q/20973/-which-equals-operator-vs-should-be-used-in-javascript-comparisonsHeathheathberry
@EarthEngine Partially incorrect: in c# == makes a reference compare only with Reference Types. For Value Types the comparison involves the in-memory values. Otherwise with int a = 3; var b = (a == 3); b would always be false ;)Open
@Open The fact is that == in c# only makes reference compare for reference types (class types) when there is no custom == operator defined (operator==). You can see this is especially true for string: it is defined as a value compare, not reference compare, because the existence of overloaded operator.Borszcz
@EarthEngine Sure thing, but all system defined value types call in a value comparison inside their operator== overloads. More in general as you now say it is true that the == behavior is decided inside the method body. But when you before said (5 yrs ago XD sry) that == only makes reference comparison it was not correct ;)Open
For instance you could make a reference type operator== overload that will match against in-memory objects values!Open
In fact I have already agreed your opinion in my last comment, and gives another counter-example: string is reference object, but its == compared by value, not by reference.Borszcz
M
1211

=== and !== are strict comparison operators:

JavaScript has both strict and type-converting equality comparison. For strict equality 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). NaN is not equal to anything, including NaN. 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.
  • Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]

Comparison Operators - MDC

Murat answered 7/2, 2009 at 11:55 Comment(5)
So, if I do for example: if (input == null) ..., will it also make the condition true when input is undefined?Yoakum
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.Anethole
-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 onesInfernal
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..?Rejoice
For plain English description of the issue see https://mcmap.net/q/20973/-which-equals-operator-vs-should-be-used-in-javascript-comparisonsHeathheathberry

© 2022 - 2025 — McMap. All rights reserved.