Equality (==)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Theequality (==) operator checks whether its two operands are equal,returning a Boolean result.Unlike thestrict equality operator,it attempts to convert and compare operands that are of different types.
In this article
Try it
console.log(1 == 1);// Expected output: trueconsole.log("hello" == "hello");// Expected output: trueconsole.log("1" == 1);// Expected output: trueconsole.log(0 == false);// Expected output: trueSyntax
x == yDescription
The equality operators (== and!=) provide theIsLooselyEqual semantic. This can be roughly summarized as follows:
- If the operands have the same type, they are compared as follows:
- Object: return
trueonly if both operands reference the same object. - String: return
trueonly if both operands have the same characters in the same order. - Number: return
trueonly if both operands have the same value.+0and-0are treated as the same value. If either operand isNaN, returnfalse; so,NaNis never equal toNaN. - Boolean: return
trueonly if operands are bothtrueor bothfalse. - BigInt: return
trueonly if both operands have the same value. - Symbol: return
trueonly if both operands reference the same symbol.
- Object: return
- If one of the operands is
nullorundefined, the other must also benullorundefinedto returntrue. Otherwise returnfalse. - If one of the operands is an object and the other is a primitive,convert the object to a primitive.
- At this step, both operands are converted to primitives (one of String, Number, Boolean, Symbol, and BigInt). The rest of the conversion is done case-by-case.
- If they are of the same type, compare them using step 1.
- If one of the operands is a Symbol but the other is not, return
false. - If one of the operands is a Boolean but the other is not,convert the boolean to a number:
trueis converted to 1, andfalseis converted to 0. Then compare the two operands loosely again. - Number to String:convert the string to a number. Conversion failure results in
NaN, which will guarantee the equality to befalse. - Number to BigInt: compare by their mathematical value. If the number is ±Infinity or
NaN, returnfalse. - String to BigInt: convert the string to a BigInt using the same algorithm as the
BigInt()constructor. If conversion fails, returnfalse.
Loose equality issymmetric:A == B always has identical semantics toB == A for any values ofA andB (except for the order of applied conversions).
The most notable difference between this operator and thestrict 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 returnsfalse for all other cases.
There's a "willful violation" of the above algorithm: if one of the operands isdocument.all, it is treated as if it'sundefined. This means thatdocument.all == null istrue, butdocument.all === undefined && document.all === null isfalse.
Examples
>Comparison with no type conversion
1 == 1; // true"hello" == "hello"; // trueComparison with type conversion
"1" == 1; // true1 == "1"; // true0 == false; // true0 == null; // false0 == undefined; // false0 == !!null; // true, look at Logical NOT operator0 == !!undefined; // true, look at Logical NOT operatornull == undefined; // trueconst number1 = new Number(3);const number2 = new Number(3);number1 == 3; // truenumber1 == number2; // falseComparison of objects
const object1 = { key: "value",};const object2 = { key: "value",};console.log(object1 == object2); // falseconsole.log(object1 == object1); // trueComparing strings and String objects
Note that strings constructed usingnew String() are objects. If youcompare one of these with a string literal, theString object will beconverted to a string literal and the contents will be compared. However, if bothoperands areString objects, then they are compared as objects and mustreference 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); // trueconsole.log(string1 == string3); // trueconsole.log(string2 == string3); // trueconsole.log(string3 == string4); // falseconsole.log(string4 == string4); // trueComparing Dates and strings
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); // trueComparing arrays and strings
const a = [1, 2, 3];const b = "1,2,3";a == b; // true, `a` converts to stringconst c = [true, 0.5, "hey"];const d = c.toString(); // "true,0.5,hey"c == d; // trueSpecifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-equality-operators> |