The == operator checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality operator, it attempts to convert and compare operands that are of different types.
The == operator checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality operator, it attempts to convert and compare operands that are of different types.
x == y
The equality operators (== and !=) provide the IsLooselyEqual semantic. This can be roughly summarized as follows:
true only if both operands reference the same object.true only if both operands have the same characters in the same order.true only if both operands have the same value. +0 and -0 are treated as the same value. If either operand is NaN, return false; so, NaN is never equal to NaN.true only if operands are both true or both false.true only if both operands have the same value.true only if both operands reference the same symbol.null or undefined, the other must also be null or undefined to return true. Otherwise return false.false.true is converted to 1, and false is converted to 0. Then compare the two operands loosely again.NaN, which will guarantee the equality to be false.NaN, return false.BigInt() constructor. If conversion fails, return false.Loose equality is symmetric: A == B always has identical semantics to B == A for any values of A and B (except for the order of applied conversions).
The most notable difference between this operator and the strict equality (===) operator is that the strict equality operator does not attempt type conversion. Instead, the strict equality operator always considers operands of different types to be different. The strict equality operator essentially carries out only step 1, and then returns false for all other cases.
There's a "willful violation" of the above algorithm: if one of the operands is document.all, it is treated as if it's undefined. This means that document.all == null is true, but document.all === undefined && document.all === null is false.
1 == 1; // true "hello" == "hello"; // true
"1" == 1; // true 1 == "1"; // true 0 == false; // true 0 == null; // false 0 == undefined; // false 0 == !!null; // true, look at Logical NOT operator 0 == !!undefined; // true, look at Logical NOT operator null == undefined; // true const number1 = new Number(3); const number2 = new Number(3); number1 == 3; // true number1 == number2; // false
const object1 = { key: "value", }; const object2 = { key: "value", }; console.log(object1 == object2); // false console.log(object1 == object1); // true
Note that strings constructed using new String() are objects. If you compare one of these with a string literal, the String object will be converted to a string literal and the contents will be compared. However, if both operands are String objects, then they are compared as objects and must reference the same object for comparison to succeed:
const string1 = "hello"; const string2 = String("hello"); const string3 = new String("hello"); const string4 = new String("hello"); console.log(string1 == string2); // true console.log(string1 == string3); // true console.log(string2 == string3); // true console.log(string3 == string4); // false console.log(string4 == string4); // true
const d = new Date("1995-12-17T03:24:00"); const s = d.toString(); // for example: "Sun Dec 17 1995 03:24:00 GMT-0800 (Pacific Standard Time)" console.log(d == s); //true
const a = [1, 2, 3]; const b = "1,2,3"; a == b; // true, `a` converts to string const c = [true, 0.5, "hey"]; const d = c.toString(); // "true,0.5,hey" c == d; // true
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
Equality |
1 | 12 | 1 | 3 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1.0 | 0.10.0 | |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality