---
title: "What is the difference between == and === in JavaScript?"
description: "== converts types before comparing, while === compares type and value with no conversion. Learn the coercion rules, the surprising cases and when == null is still useful."
url: https://www.danny.engineering/answer/javascript/what-is-the-difference-betweenandin-javascript
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["Javascript"]
---

# What is the difference between == and === in JavaScript?

## The short answer


`===` (strict equality) is true only when both sides have the same type and the same value. `==` (loose equality) first converts the values to a common type and then compares them, which produces some surprising results. Use `===` and `!==` by default.


```javascript
1 === 1;     // true
1 === '1';   // false, different types
1 == '1';    // true, the string is converted to a number
0 == '';     // true
null == undefined;  // true
null === undefined; // false
```


## Why it happens


Loose equality follows an algorithm in the language specification that tries to make the two sides comparable. The main rules are:

- If both values have the same type, `==` behaves like `===`.
- `null` and `undefined` are equal to each other and to nothing else.
- If one side is a number and the other a string, the string is converted to a number.
- A boolean is converted to a number first, so `true` becomes `1` and `false` becomes `0`.
- If one side is an object and the other a primitive, the object is converted to a primitive, usually through `valueOf` or `toString`.

Those steps chain together, which is where the odd results come from:


```javascript
'0' == false;   // true: false becomes 0, '0' becomes 0
[] == false;    // true: [] becomes '' becomes 0
[1] == 1;       // true: [1] becomes '1' becomes 1
' \t\n' == 0;   // true: whitespace-only strings become 0
'0' == '';      // false: both are strings, so no conversion
```


Notice that `==` is not even transitive: `'0' == 0` and `0 == ''` are both true, but `'0' == ''` is false.


Strict equality skips all of this. If the types differ, the answer is `false`.


## How to fix it


### Use === and !== everywhere


Strict comparisons are easier to read, because the result never depends on hidden conversions. Turn on the `eqeqeq` rule in ESLint to catch accidental `==`.


```json
{
  "rules": {
    "eqeqeq": ["error", "always", { "null": "ignore" }]
  }
}
```


The `null: ignore` setting allows the one common exception below.


### Convert explicitly when types differ


If you really do have a string and need to compare it with a number, for example a value from a form field or a URL, convert it yourself so the intent is clear.


```javascript
const page = Number(searchParams.get('page'));

if (page === 1) {
  // ...
}
```


### Use == null to check for null or undefined


Because `null` and `undefined` are only loosely equal to each other, `value == null` is a short way to test for either. Many style guides allow this one use.


```javascript
function greet(name) {
  if (name == null) return 'Hello, stranger';
  return `Hello, ${name}`;
}
```


If your team prefers strict checks throughout, `value === null || value === undefined` does the same thing, and the `??` operator often removes the need for the check altogether.


### Use [Object.is](http://object.is/) for the edge cases


Even `===` has two quirks: `NaN === NaN` is `false`, and `0 === -0` is `true`. `Object.is` handles both.


```javascript
NaN === NaN;          // false
Object.is(NaN, NaN);  // true
Number.isNaN(value);  // the usual way to test for NaN
```


## Common pitfalls

- Expecting `===` to compare objects by content. Two separate objects or arrays are never equal, even with identical contents, because both operators compare references for objects.
- Writing `if (x == true)`. For any value other than `true` or `1` this is usually false, even for truthy values like `'yes'`. Write `if (x)` or compare with `===`.
- Comparing form or query values without converting them. They are always strings, so `input.value === 5` is never true.
- Assuming `==` is faster. Modern engines optimise both, so there is no reason to pick `==` for speed.
- Using `switch` and expecting loose matching. `switch` uses strict comparison, so `case 1` does not match `'1'`.

## Related reading

- [MDN: Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness)
- [MDN: Strict equality (===)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality)
