Featurecore
Loose equality
Also called ==, !=, double equals, abstract equality, IsLooselyEqual, loose inequality.
x == y converts operands of different types before it compares them. null and undefined equal only each other, Booleans become numbers, and objects become primitives.
- Language
- JavaScript
Synopsis
x == y
x != y
| Operands | Step |
|---|---|
| same type | compare as === |
null and undefined |
true; each is equal only to the other |
| number and string | convert the string to a number |
| BigInt and string | convert the string to a BigInt; false if that fails |
| a Boolean and any other value | convert the Boolean to a number |
| an object and a primitive | convert the object to a primitive |
| number and BigInt | compare the math values; false for NaN and infinities |
| anything else | false |
"The equality (==) 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."1
console.log("1" == 1, 0 == false, null == undefined, 0 == null);
console.log(1n == 1, "2" == 2n, [1] == 1, new String("a") == "a");
console.log(NaN == NaN, "" == 0, "0" == false, [] == false);
true true true false
true true true true
false true true true
Semantics
r1. Same type. "If SameType ( x , y ) is true , then Return IsStrictlyEqual ( x , y )."2 Two objects are equal only when they are the same object.1
r2. null and undefined. "If one of the operands is null or undefined, the other must also be null or undefined to return true."1 So 0 == null is false.1
r3. Numbers and strings. The string becomes a number. "Conversion failure results in NaN, which will guarantee the equality to be false."1
r4. Booleans. "If one of the operands is a Boolean but the other is not, convert the boolean to a number: true is converted to 1, and false is converted to 0. Then compare the two operands loosely again."1 Example: boolean.
r5. Objects. An object compared with a string, number, BigInt or symbol goes through ToPrimitive.2 See Type coercion.
r6. BigInt. For a number and a BigInt, "If x is not finite or y is not finite, return false." Else the math values are compared.2 A string is converted with StringToBigInt, and failure gives false.2
r7. Symmetry. "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)."3
r8. Not transitive. '' == 0 and 0 == '0' are true, but '' == '0' is false, because two strings compare as strings (rule r1). Example: transitive.
Examples
The examples run with Node.js 24.
A Boolean is a number in == (rule r4):
console.log("1" == true, "2" == true, "true" == true);
console.log(Boolean("2"), Boolean("true"));
true false false
true true
== is not transitive (rule r8):
console.log("" == 0, 0 == "0", "" == "0");
true true false
One comparison tests for both null and undefined (rule r2):
for (const v of [null, undefined, 0, "", false]) {
const name = v === "" ? "(empty)" : String(v);
console.log(name.padEnd(9), v == null);
}
null true
undefined true
0 false
(empty) false
false false
Pitfalls
== true is not truthiness
'2' == true is false, but '2' is truthy (rule r4). Write if (x) to test truthiness. See Booleans and truthiness.
Two wrapper objects
new Number(3) == new Number(3) is false, since both operands are objects (rule r1).1
Prefer ===
"In most cases, using loose equality is discouraged. The result of a comparison using strict equality is easier to predict, and may evaluate more quickly due to the lack of type coercion."3 x == null is one common use, as in nullish.
Comparison
| Topic | JavaScript == |
Python == |
|---|---|---|
| different types | converts, then compares1 | "Unless stated otherwise, objects of different types never compare equal."4 |
'1' and 1 |
true1 | false; a string and a number are different types4 |
| Booleans and numbers | true == 1 is true1 |
"bool is a subclass of int", so True == 14 |
Python has one equality operator with no string to number step.4 JavaScript keeps == for this conversion and === without it.1 See Comparing sequences and other types.
References
- Equality (==), MDN, Description; Examples
- ECMAScript 2026 Language Specification, 7.2.13 IsLooselyEqual
- Equality comparisons and sameness, MDN, Loose equality using ==
- Built-in Types, The Python Standard Library, Comparisons; Boolean Type - bool
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 0NR2CH Edit this page History Report an error Markdown