1663

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

Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
Shiva
  • 17,795
  • 4
  • 18
  • 9
  • 1
    `==` is `===` with type converting (aka coercion). To really understand what I mean you can look at this JavaScript function that behaves exactly like `==`: http://stackoverflow.com/a/38856418/984780 – Luis Perez Aug 09 '16 at 17:17
  • Some [possibly surprising examples of `==`](https://stackoverflow.com/a/47015438/199364). – ToolmakerSteve Oct 27 '20 at 19:41
  • 1
    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. – Sebastian Simon Mar 15 '21 at 08:41

2 Answers2

1983

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
meager
  • 209,754
  • 38
  • 307
  • 315
sdfx
  • 21,123
  • 4
  • 23
  • 34
  • 83
    Thanks for the clear answer! I guess if compared to C# the == would also be == and === would translate to .Equals() – Koen Zomers Feb 01 '11 at 13:02
  • 12
    what about "new String()===new String()", both values and types are same. But statement returns false. – hrishikeshp19 Sep 18 '12 at 00:36
  • 41
    @hrishikeshp19: in that case, the values are actually different (different object references) – l8nite Oct 23 '12 at 03:27
  • 19
    @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. – Earth Engine Nov 22 '12 at 02:37
  • 5
    @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`. – danorton Feb 14 '13 at 23:26
  • Also '0' == false // true and null == undefined //true – Pablo Jomer Jul 22 '14 at 08:23
  • 3
    Can someone explain why `true == "true"` is `false`? – Alexandru Severin Mar 09 '15 at 09:54
  • 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// number – pixel 67 Jun 26 '15 at 10:39
  • Is there a speed difference between `== -1` and `=== -1` for `anArray.indexOf(el)`? – Qwerty Jul 30 '16 at 13:36
  • 1
    @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' === true` – Qwerty Jul 30 '16 at 13:45
  • @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. – Qwerty Jul 30 '16 at 13:47
  • @sdfx 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 ? – Dror Dec 08 '16 at 07:04
  • For more unexpected examples check out http://stackoverflow.com/a/38856418/984780 – Luis Perez Mar 02 '17 at 23:09
  • @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 ;) – Shockwaver Jun 29 '17 at 08:48
  • @Shockwaver 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. – Earth Engine Jun 29 '17 at 11:11
  • @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 ;) – Shockwaver Jun 29 '17 at 15:01
  • For instance you could make a reference type `operator==` overload that will match against in-memory objects values! – Shockwaver Jun 29 '17 at 15:08
  • 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. – Earth Engine Jun 29 '17 at 23:57
  • @riship89 Never declare Primitives (Number, String , Boolean etc) as objects as it produces nasty side effects eg. var x = "John"; var y = new String("John"); x===y will give false. – Eggineer Mar 07 '18 at 09:44
1200

=== 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

meager
  • 209,754
  • 38
  • 307
  • 315
Jack Sleight
  • 16,671
  • 6
  • 38
  • 53
  • 4
    So, if I do for example: `if (input == null) ...`, will it also make the condition true when input is undefined? – Filip Vondrášek Jan 25 '13 at 23:42
  • 5
    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. – Matt Browne Feb 10 '13 at 01:44
  • 186
    -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 – CodyBugstein Mar 20 '14 at 07:21
  • 7
    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..? – T J Sep 30 '14 at 08:49
  • 1
    For plain English description of the issue see http://stackoverflow.com/a/38856418/984780 – Luis Perez Mar 02 '17 at 23:08