== vs === Equality Operators

Difficulty: Beginner

Question

What is the difference between == and === in JavaScript, and when, if ever, should you use ==?

Answer

=== (strict equality) returns true only if both operands have the same type and the same value, with no conversion. == (loose equality) first converts the operands to a common type using the Abstract Equality rules and then compares, which produces surprising results like '' == 0 being true. Use === by default; the one widely accepted use of == is value == null to check for both null and undefined.

How == converts values

The loose equality algorithm in the spec boils down to a few rules:

  1. Same type: compare like ===.
  2. null == undefined is true, and neither equals anything else (null == 0 is false).
  3. Number and string: the string is converted to a number ('42' == 42 is true).
  4. Boolean and anything: the boolean is converted to a number first (true becomes 1), so true == '1' is true but true == 'yes' is false.
  5. Object and primitive: the object is converted with valueOf/toString (ToPrimitive), then compared again. [1] == 1 is true because [1] becomes '1' and then 1.

Results that trip people up

Expression=====
0 == ''truefalse
0 == '0'truefalse
'' == '0'falsefalse
false == '0'truefalse
null == undefinedtruefalse
null == 0falsefalse
[] == falsetruefalse
NaN == NaNfalsefalse

Notice that == is not transitive: 0 == '' and 0 == '0', yet '' != '0'. That alone is a good reason to avoid it.

Objects compare by reference either way

Neither operator compares structure. { a: 1 } === { a: 1 } and [1, 2] == [1, 2] are both false because they are different objects. Two variables pointing at the same object are equal. For value comparison you need a deep equality helper, or for simple data JSON.stringify (which is order-sensitive and has limits).

Edge cases and Object.is

=== has two quirks: NaN === NaN is false, and +0 === -0 is true. Object.is fixes both: Object.is(NaN, NaN) is true and Object.is(0, -0) is false. React uses Object.is to decide whether state or dependencies changed. To test for NaN, use Number.isNaN(x).

What teams do in practice

Watch == convert its operands step by step. Try [] == false, "5" == 5, null == 0 and NaN === NaN under Tweak.
  1. Boolean → number: false becomes 0.
  2. Object → primitive: [] becomes "" through toString().
  3. String → number: "" becomes 0.
  4. Compare: 0 == 0, so [] == false is true. === would return false immediately because the types differ.

Code examples

Loose vs strict in action

console.log('42' == 42, '42' === 42);       // true false
console.log(0 == '', 0 == '0', '' == '0');  // true true false
console.log(false == '0', [] == false);     // true true
console.log(null == undefined, null === undefined); // true false
console.log(null == 0, null >= 0);          // false true (relational ops convert null to 0)

== converts types before comparing, which leads to non-transitive results. The last line shows null behaving differently under == and >=.

The accepted == null idiom and Object.is

function isMissing(v) {
  return v == null; // true for null and undefined only
}
console.log(isMissing(null), isMissing(undefined), isMissing(0), isMissing(''));

console.log(NaN === NaN, Object.is(NaN, NaN));  // false true
console.log(0 === -0, Object.is(0, -0));        // true false

const a = { id: 1 };
const b = { id: 1 };
console.log(a === b, a === a);                  // false true

v == null is a concise check for both nullish values. Object.is handles NaN and signed zero, and objects are only equal to themselves.

Key points

Common follow-up questions

Interview tips

Concepts covered

Strict Equality, Loose Equality, Type Coercion, Object.is, Abstract Equality Algorithm

More JavaScript Interview questions