Strict vs Loose Equality
There are two equality operators in JavaScript and the difference between them is the single most useful rule a beginner can learn.
Triple equals compares value and type with no conversion. Double equals converts first, using rules with enough exceptions that nobody remembers them. Use === always, with one narrow exception for == null.
EXERCISE
1Triple Equals Compares Without Converting
The strict equality operator asks one simple question: are these the same type, and the same value.
=== answers true only when both the type and the value match. There is no conversion, no cleverness and no table to memorise.
Same type, same value:
console.log(5 === 5); // true
console.log("a" === "a"); // true
console.log(true === true); // true
console.log(null === null); // true
Different type is always false, whatever the value looks like:
console.log(5 === "5"); // false number vs string
console.log(0 === false); // false number vs boolean
console.log("" === 0); // false
console.log(null === undefined); // false
And the inverse, written with an exclamation mark:
console.log(5 !== "5"); // true they are NOT strictly equal
console.log(5 !== 5); // false
const status = "active";
if (status !== "active") {
console.log("not active");
}
// Nothing prints. status is exactly the string "active".
The one thing === cannot do:
console.log(NaN === NaN); // false as always
console.log({ a: 1 } === { a: 1 }); // false different objects
You can describe === in one sentence and be completely correct: same type and same value. That is the whole specification. The reason it is the recommended operator is not that it is stricter, it is that it is the one you can reason about without looking anything up.
EXERCISE
2Double Equals Converts First, and the Rules Are Odd
Loose equality tries to make both sides comparable before it compares them, using a set of rules with real surprises in it.
== converts and then compares. Often that is what you wanted. Sometimes it is not, and the exceptions are not guessable.
The reasonable-looking cases:
console.log(5 == "5"); // true "5" becomes 5
console.log(1 == true); // true true becomes 1
console.log(0 == false); // true false becomes 0
The cases nobody would guess:
console.log("" == 0); // true
console.log("" == false); // true
console.log(" " == 0); // true a space becomes 0
console.log("0" == false); // true
console.log([] == false); // true an empty array becomes ""
console.log([] == 0); // true
console.log([1] == 1); // true
console.log(null == 0); // false <- but null >= 0 is true
console.log(null == undefined); // true the one useful exception
And the failure of transitivity, which is genuinely alarming:
console.log("0" == 0); // true
console.log(0 == ""); // true
console.log("0" == ""); // false !
// If a equals b and b equals c, a should equal c.
// With == that guarantee simply does not hold.
The one place == earns its keep:
let value = null;
// "Is this null or undefined?" in one comparison:
console.log(value == null); // true
value = undefined;
console.log(value == null); // true
value = 0;
console.log(value == null); // false <- 0 is not caught
// The strict version needs two checks:
console.log(value === null || value === undefined); // false
Pro Tip: Use
===everywhere. The single justified use of==isvalue == null, which catches null and undefined together and nothing else, and even that is a convention you should write deliberately rather than by accident. Every other use of==is a question that only its author can answer.
EXERCISE
3Comparing Things That Are Not Simple Values
Objects and arrays never compare equal by contents, so checking whether two of them hold the same data takes deliberate work.
=== on an object asks are these the same object in memory, not are they alike.
Two identical-looking objects are not equal:
const a = { name: "Ada" };
const b = { name: "Ada" };
console.log(a === b); // false two separate objects
console.log(a == b); // false == does not help either
const c = a;
console.log(a === c); // true one object, two names
The same is true of arrays, which are objects:
console.log([1, 2, 3] === [1, 2, 3]); // false
console.log([] === []); // false
// Which is why this common check fails:
const items = [];
if (items === []) {
console.log("never runs");
}
// Check the length instead:
console.log(items.length === 0); // true
Comparing contents, for simple flat data:
const p = { id: 1, name: "Ada" };
const q = { id: 1, name: "Ada" };
// Compare the fields you care about:
console.log(p.id === q.id && p.name === q.name); // true
// Or, for small flat objects with no nesting and no functions:
console.log(JSON.stringify(p) === JSON.stringify(q)); // true
// That trick is fragile: key order matters, and dates,
// undefined values and functions do not survive the round trip.
There is no built-in deep equality in , and that is deliberate, because "the same data" means different things in different programs. Compare the specific fields that matter to your comparison. When you genuinely need a general answer, that is what a library such as lodash's isEqual is for.
