Glossary / Equality: == and ===

Equality: == and ===

JavaScript provides abstract equality (==) and strict equality (===). Strict equality compares without cross-type coercion and is the default choice for most application logic. Abstract equality applies a defined set of conversions and exceptions; it is not random, but it is harder to reason about and can hide mismatched data types.

Strict equality

For primitive values, === requires compatible types and equal values, with Number edge cases: NaN is unequal to itself, while positive and negative zero compare equal. For objects and functions, strict equality compares identity, not structure.

1 === "1"; // false
({ id: 1 }) === ({ id: 1 }); // false: two different objects
const record = {};
record === record; // true

If domain equality means “same user ID” or “same coordinates within tolerance,” implement that rule explicitly. JavaScript has no universal structural equality because prototypes, cycles, symbols, property order, and domain semantics complicate the question.

Abstract equality

== recursively applies the specification’s abstract equality algorithm. A few cases matter in practice: null == undefined is true; strings and numbers may be converted; booleans are converted to numbers; objects can be converted to primitives. The rules have historical edge cases (document.all is a legacy web-compatibility exception), so broad loose comparisons are poor input validation.

A narrow idiom is value == null, which intentionally matches both null and undefined and no other ordinary values. Use it only when this dual check is deliberate and accepted by the project style. Otherwise write value === null || value === undefined or use value ?? fallback for a default.

Interview questions

  1. What does 1 == "1" return? True, after abstract equality converts the string to a number.
  2. How do objects compare with ===? By reference identity.
  3. What are the Number edge cases for strict equality? NaN is unequal to itself; +0 and -0 are equal.
  4. Is == always wrong? No, but strict equality is clearer by default; == null is a sometimes-used explicit nullish check.
  5. How compare two objects by domain meaning? Define a domain-specific comparison over the meaningful fields or stable identifiers.

References