# == vs === Equality Operators

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

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-equality-operators
Last updated: 2026-11-28

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 == ''` | `true` | `false` |
| `0 == '0'` | `true` | `false` |
| `'' == '0'` | `false` | `false` |
| `false == '0'` | `true` | `false` |
| `null == undefined` | `true` | `false` |
| `null == 0` | `false` | `false` |
| `[] == false` | `true` | `false` |
| `NaN == NaN` | `false` | `false` |

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

- ESLint's `eqeqeq` rule is enabled in most style guides, often with the `{ null: 'ignore' }` option to allow `x == null`.
- TypeScript catches many accidental comparisons between unrelated types at compile time, but runtime values from forms and APIs are still strings, so convert explicitly: `Number(input) === 0`.
- Form fields, query params and `localStorage` always return strings, which is exactly where `==` hides bugs.

**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

- === compares type and value with no conversion
- == applies coercion rules and is not transitive
- x == null is the one common, deliberate use of ==
- Objects are compared by reference under both operators
- Object.is differs from === only for NaN and +0/-0

## Common follow-up questions

- Why does \[\] == !\[\] evaluate to true?
- How does React decide if state has changed?
- How would you write a deep equality function?
- Why is null >= 0 true while null == 0 is false?

## Interview tips

- Explain the conversion rules briefly instead of just saying '== is bad'
- Mention x == null as the deliberate exception; it shows nuance
- Bring up Object.is and NaN; it often earns a follow-up about React

## Concepts covered

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

Previous: [Error Handling (try/catch/finally)](https://www.internhack.in/learn/interview/javascript-interview/js-error-handling) · Next: [Arrow Functions vs Regular Functions](https://www.internhack.in/learn/interview/javascript-interview/js-arrow-functions) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [Template Literals & String Interpolation](https://www.internhack.in/learn/interview/javascript-interview/js-template-literals)
- [null vs undefined](https://www.internhack.in/learn/interview/javascript-interview/js-null-undefined)
- [JSON: parse, stringify & Common Pitfalls](https://www.internhack.in/learn/interview/javascript-interview/js-json-parse-stringify)
- [Closures & Lexical Scope](https://www.internhack.in/learn/interview/javascript-interview/js-closures-interview)
- [Hoisting & Temporal Dead Zone](https://www.internhack.in/learn/interview/javascript-interview/js-hoisting-interview)
- [Object Methods & Property Descriptors](https://www.internhack.in/learn/interview/javascript-interview/js-object-methods)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
