# Strict vs loose equality in JavaScript

> JavaScript has two equality operators: === checks both value and type, while == only checks value with type coercion. Here's when to use each one.

Published: 2023-01-20T12:39:30.000Z
Updated: 2026-04-24T10:00:00.000Z
Author: Shameem Reza
Category: Development
Canonical: https://shameemreza.com/strict-vs-loose-equality-in-javascript/

---

import Tldr from '../../components/Tldr.astro';

JavaScript has two equality operators that look almost identical but behave very differently: `===` (strict) and `==` (loose). Strict compares value and type. Loose compares value after coercing the types to match. The result is that `1 === "1"` is `false` and `1 == "1"` is `true`. That one-character difference can change how your code behaves, so it's worth knowing exactly when to use each.

<Tldr>
  Use `===` by default. It compares both value and type, so `1 === "1"` is `false` and there are no coercion surprises. Reach for `==` only when you want a string and a number version of the same value to match, or when you want `null` and `undefined` to match in a single check. If your project has ESLint, turn on the `eqeqeq` rule and stop thinking about it.
</Tldr>

## Strict equality (===)



Strict equality, also known as "triple equals", compares both the value and the type of two operands. It returns true if the operands are equal and of the same type.

![JavaScript strict equality comparison table](/uploads/strict-equality.png)

Source: [JavaScript equality table by Dorey](https://dorey.github.io/JavaScript-Equality-Table/).

Strict equality example:


```
1 === 1 // true
1 === "1" // false
```



In the first example, both operands are of the same type (number) and have the same value, so the comparison returns true. In the second example, one operand is a number and the other is a string, so the comparison returns false.


## Loose equality (==)



Loose equality, also known as "double equals", compares the values of two operands without considering their types. It returns true if the operands are equal, regardless of their type.

![JavaScript loose equality comparison table](/uploads/loose-equality.png)

Source: [JavaScript equality table by Dorey](https://dorey.github.io/JavaScript-Equality-Table/).

Loose equality example:


```
1 == 1 // true
1 == "1" // true
```



In the first example, both operands are of the same type (number) and have the same value, so the comparison returns true. In the second example, one operand is a number and the other is a string, but their values are the same, so the comparison returns true.


## When to use each one



In general, it's best to use strict equality whenever possible because it avoids the confusion that type coercion can cause. There are a few cases where loose equality is useful:

- When comparing a variable to `null` or `undefined`.
- When comparing a variable to a boolean.
- When comparing a variable that may be a string or a number.




```
// Use strict equality when comparing variables of the same type
let age = 25;
let legalAge = 21;
console.log(age === legalAge); // false

// Use loose equality when comparing variables that may be a string or a number
let input = "25";
let number = 25;
console.log(input == number); // true

// Use loose equality when comparing a variable to null or undefined
let name;
console.log(name == undefined); // true
```



A simple rule: default to `===`. It's predictable, it avoids the type coercion traps in the [MDN sameness chart](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness), and it makes your intent clear. Reach for `==` only when you genuinely need to treat string and number versions of the same value as equal, or when you want `null` and `undefined` to match in a single check. In [JavaScript](/typescript-vs-javascript-what-are-the-differences/) projects with a linter, `eslint`'s `eqeqeq` rule will flag every `==` for you, which is a good safety net.

**More resources:**

- [MDN web docs: Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness)
- [JavaScript Equality Table by w3schools.com](https://www.w3schools.com/js/js_comparisons.asp)
- [JavaScript Equality Comparison table by Dorey](https://dorey.github.io/JavaScript-Equality-Table/)
