Full answer
=== is strict equality. If the two values have different types, the answer is false at once. If they have the same type, the values are compared. Objects and arrays are compared by reference, so two separate objects are never equal even when their contents match.
== is loose equality. When the types differ, the engine converts one or both sides following the rules in the language specification, then compares. A string and a number are both turned into numbers. A boolean is turned into a number first, so true becomes 1. An object is turned into a primitive using its valueOf or toString methods. null and undefined are equal to each other and to nothing else.
Because the rules are long, results can look random. The empty array [] becomes the empty string, then the number 0, and so [] == false is true. Yet [] on its own is truthy, which is a different test.
The practical advice is to use === and !== by default so the comparison says exactly what it means. The one accepted use of == is value == null, which checks for both null and undefined in one short test.
NaN is not equal to itself under either operator. Use Number.isNaN(value) or Object.is(value, NaN).
console.log('5 == "5":', 5 == '5');
console.log('5 === "5":', 5 === '5');
console.log('0 == false:', 0 == false);
console.log('"" == false:', '' == false);
console.log('null == undefined:', null == undefined);
console.log('null == 0:', null == 0);
console.log('[] == false:', [] == false);
console.log('[] is truthy:', Boolean([]));
console.log('{} == {}:', {} == {});
console.log('NaN == NaN:', NaN == NaN);
console.log('Object.is(NaN, NaN):', Object.is(NaN, NaN));
const input = undefined;
console.log('input == null:', input == null);Run with Node.js v24.22.1 as js-3.js; two runs gave identical output. Output: 5 == "5": true 5 === "5": false 0 == false: true "" == false: true null == undefined: true null == 0: false [] == false: true [] is truthy: true {} == {}: false NaN == NaN: false Object.is(NaN, NaN): true input == null: true
Key points
- === never converts types; == converts before comparing.
- null == undefined is true, but null and undefined are not equal to 0, false, or an empty string under ==.
- Objects and arrays are compared by reference under both operators.
- NaN is not equal to anything, including itself, under both operators.
- Prefer ===; value == null is the one widely accepted use of ==.
Common mistakes
- Claiming that == is always wrong. The == null check is common and deliberate.
- Expecting two objects with the same fields to be equal under ===.
- Assuming null == 0 is true because null "feels like zero". It is false; null only loosely equals undefined.
- Using === to compare with NaN and being surprised by false.
Follow-ups
- Why is null >= 0 true while null == 0 is false?
- How does Object.is differ from ===?
- What happens when an object with a custom valueOf is compared with a number using ==?
Tips
- Do not try to recite the full coercion table. Explain the idea (convert, then compare) and give three crisp examples.
- Bring up == null as the exception yourself. It shows judgment.
- If asked about [] == ![] , walk through it step by step: ![] is false, then both sides become 0.