Loose Equality vs Strict Equality in JavaScript Explained
Strict equality compares type and value, and loose equality converts types first by a fixed algorithm. See the rules, a results table and the one use to keep.

Strict equality does what it says. Loose equality does what a table in the specification says, and the table is long enough that nobody carries it in their head. That is the entire case against ==: not that it is always wrong, but that reading it correctly takes work the reader should not have to do. This post lays out the algorithm in the order the engine applies it, shows the results that surprise people, and ends on the single use that earns a place in modern code. Examples run on Node.js 24. The conversions the algorithm leans on are covered in how type coercion works, and the whole topic sits inside the guide to types and coercion.
What strict equality checks
=== compares the types first, and if they differ it returns false without converting anything. If they match, it compares the values by the rules for that type.
console.log(1 === 1);
console.log("1" === 1);
console.log(NaN === NaN);
console.log(0 === -0);
console.log("a" === "a");
const a = { id: 1 };
console.log(a === a);
console.log({ id: 1 } === { id: 1 });true
false
false
true
true
true
falseThere is nothing to remember beyond two exceptions: NaN is never equal to anything, and 0 equals -0. Objects are equal only to themselves. The result of === can always be predicted by looking at the two operands.
What loose equality does
The == operator applies these steps in order. If the types are the same, it behaves as ===. If one operand is null and the other undefined, the result is true. If one is a number and the other a string, the string becomes a number. If either is a boolean, it becomes a number, 1 or 0. If one is an object and the other a primitive, the object is converted to a primitive. A bigint is compared by mathematical value against numbers and strings.
The steps repeat until both sides have the same type or the comparison is decided. The sequence is why results look arbitrary: an operand can be converted two or three times on the way.
const pairs = [
[1, "1"],
[0, ""],
[0, "0"],
["", "0"],
[false, "0"],
[false, ""],
[null, undefined],
[null, 0],
[undefined, 0],
[[], false],
[[1], 1],
[NaN, NaN],
];
const show = (value) =>
typeof value === "string"
? `"${value}"`
: Array.isArray(value)
? `[${value}]`
: String(value);
for (const [left, right] of pairs) {
console.log(`${show(left)} == ${show(right)}`, left == right);
}1 == "1" true
0 == "" true
0 == "0" true
"" == "0" false
false == "0" true
false == "" true
null == undefined true
null == 0 false
undefined == 0 false
[] == false true
[1] == 1 true
NaN == NaN falseRead the third and fourth lines together. 0 == "" is true, 0 == "0" is true, and "" == "0" is false. Equality is supposed to be transitive, and this is not. Two strings are compared as strings, so "" == "0" never converts to a number, while each of them does when compared with 0.
The [] == false result takes three steps. The boolean becomes 0, the empty array becomes the empty string through its toString(), and the empty string becomes 0. The post on converting objects to primitives walks through that middle step.
null and undefined are special
The pairing of null and undefined is the one part of the table that is deliberate and useful. They are loosely equal to each other and to no other value, including 0, "" and false.
const values = [null, undefined, 0, "", false];
console.log(values.map((value) => value == null));
console.log(values.map((value) => value === null || value === undefined));[ true, true, false, false, false ]
[ true, true, false, false, false ]Both lines give the same answer, and the first is shorter. value == null says "this is missing", and the second form writes it out. This is the exception style guides allow, because the result is easy to read and impossible to get wrong. The post on null and undefined covers why they are treated as one for this purpose, and the newer ?? and ?. operators use the same pairing.
A bug the table predicts
Form handling is where the table bites. A value read from an input is always a string, and a lookup against numeric ids with == appears to work.
const orders = [
{ id: 7, total: 40 },
{ id: 12, total: 15 },
];
const idFromUrl = "12";
console.log(orders.find((order) => order.id == idFromUrl)?.total);
console.log(orders.find((order) => order.id === idFromUrl)?.total);
console.log(orders.find((order) => order.id === Number(idFromUrl))?.total);15
undefined
15The loose version matches because the string is converted to a number, and the strict one returns undefined, which looks like the bug. It is the reverse. The loose version hides that the id was never converted, and the data will eventually arrive as "012" or "12 " or "1e1", all of which convert to a number and some of which should have been rejected. The third form converts once, at a place you can validate, and keeps the comparison strict. We trace most of the "works on my machine" id bugs to an unvalidated string compared loosely.
Bigint and the mixed cases
A bigint compares by mathematical value with a number, and strictly it is a different type.
console.log(1n == 1);
console.log(1n === 1);
console.log("1" == 1n);
console.log(2n > 1);
console.log(9007199254740993n == 9007199254740992);true
false
true
true
falseThe last line shows that the comparison is exact: the number 9007199254740992 is the nearest double to both integers, and the bigint 9007199254740993n is a different mathematical value, so the loose comparison is false. This is one place where the algorithm is more accurate than converting both sides to numbers would be, and the post on BigInt covers the rest.
Why strict equality is the default
Three reasons stack up. A strict comparison is read at a glance, which matters in code review. It avoids the non-transitive cases that cause real bugs, like a form field value "0" matching a numeric 0 and a blank field "" matching it too. And it catches type mistakes: if you expect a number and receive a string, === reports the difference, and == hides it by making them agree.
In TypeScript the compiler also flags a strict comparison of unrelated types, with an error that says the comparison appears to be unintentional because the types have no overlap. That check does not exist for == in the same way, so strict equality gets the type system working for you.
const id: number = 42;
const fromUrl: string = "42";
// Error: This comparison appears to be unintentional because the types 'number' and 'string' have no overlap.
if (id === fromUrl) {
console.log("same");
}The error points at the real bug: the id from a URL is a string. The loose version would have compiled and matched.
Enforcing it
The eqeqeq rule in ESLint rejects == and != outright, and has an option to leave the null comparison alone.
// eslint.config.js
export default [
{
rules: {
eqeqeq: ["error", "always", { null: "ignore" }],
},
},
];With null: "ignore", value == null stays legal and every other loose comparison fails the lint step. That is the configuration we use. A codebase that bans all == loses a short idiom for no gain, and one that allows it everywhere inherits the whole table.
Reviewing code that uses ==
When a pull request contains ==, the review question is which of the five rules the author is relying on. If the answer is null, approve it. If it is a number against a string, ask for the conversion to be explicit. If it is a comparison with true or false, ask for a plain truthiness test, because loose comparison with a boolean is the one case where the result depends on a type the reader cannot see. We write the review comment as a question about the types on each side, since the author has usually not thought about them, and answering the question is the fix.
A boundary: where loose equality is harmless
Comparing two values you know have the same type is the same as ===, and a reader cannot tell. That makes the operator risky even when the code is correct, because the next edit may change a type. Code that must run on a very old engine has no extra reason to prefer it either.
The one other place you will meet == is generated or legacy code, where changing it is a refactor with its own risk. If you must edit such code, replace each == only after reading the types on both sides, since a blanket replacement changes behavior wherever the old code relied on a conversion.
The algorithm is in the specification as IsLooselyEqual, and MDN's page on equality comparisons and sameness shows the full comparison table.


