# JavaScript Equality Operators: The Difference Between == and === Explained

> Understand the crucial difference between JavaScript == and === operators. Learn how == coerces types while === demands strict equality for accurate comparisons.

- Repository: [H5BP/Front-end-Developer-Interview-Questions](https://github.com/h5bp/Front-end-Developer-Interview-Questions)
- Tags: how-to-guide
- Published: 2026-03-05

---

**The `==` operator performs type coercion before comparing values, while `===` requires both value and type to match without any conversion.**

Understanding the difference between `==` and `===` in JavaScript is essential for writing predictable code and succeeding in technical interviews. The h5bp/Front-end-Developer-Interview-Questions repository explicitly tests this concept in [`src/questions/javascript-questions.md`](https://github.com/h5bp/Front-end-Developer-Interview-Questions/blob/main/src/questions/javascript-questions.md) (line 30), highlighting it as a fundamental distinction every frontend developer must master. While both operators compare values, their handling of type conversion creates dramatically different behaviors that can introduce subtle bugs in production applications.

## How Loose Equality (==) Works in JavaScript

The loose equality operator (`==`) compares two values after performing **type coercion**, which means JavaScript attempts to convert the operands to a common type before making the comparison.

### Type Coercion Rules and Edge Cases

When using `==`, JavaScript follows complex coercion rules that often produce unexpected results:

- `0 == false` evaluates to `true` because the number `0` coerces to the boolean `false`
- `'5' == 5` evaluates to `true` because the string `'5'` coerces to the number `5`
- `null == undefined` evaluates to `true` because the specification treats these as loosely equal
- `'' == 0` evaluates to `true` as the empty string coerces to the number `0`

These implicit conversions make `==` risky for most comparisons, as noted in the interview questions repository's JavaScript section.

## How Strict Equality (===) Works in JavaScript

The strict equality operator (`===`) compares both **value** and **type** without performing any type coercion. If the operands differ in type, the comparison immediately returns `false`.

### No Type Conversion Behavior

Strict equality eliminates the unpredictability of loose equality:

- `'5' === 5` returns `false` because a string does not equal a number
- `0 === false` returns `false` because a number does not equal a boolean
- `null === undefined` returns `false` because they are distinct types
- `'' === 0` returns `false` because string and number types differ

According to the h5bp/Front-end-Developer-Interview-Questions source code, using `===` is considered a best practice for most JavaScript codebases because it prevents accidental type-coercion bugs.

## Practical Code Examples

The following examples demonstrate the critical differences between loose and strict equality in JavaScript:

```javascript
// Loose equality (==) with type coercion
console.log(0 == false);          // true
console.log('5' == 5);            // true
console.log(null == undefined);   // true
console.log('' == 0);             // true
console.log([] == false);         // true

// Strict equality (===) without coercion
console.log(0 === false);         // false
console.log('5' === 5);           // false
console.log(null === undefined);  // false
console.log('' === 0);            // false
console.log([] === false);        // false

```

## When to Use == vs === in JavaScript

Choosing the appropriate equality operator depends on your specific use case, though modern JavaScript best practices strongly favor one approach.

### Best Practices for Strict Equality

Use `===` as your default comparison operator in virtually all scenarios. It provides predictable behavior, eliminates type coercion bugs, and makes your intentions explicit to other developers. The h5bp/Front-end-Developer-Interview-Questions repository explicitly tests this preference, indicating that interviewers expect candidates to prefer strict equality for most comparisons.

### Legitimate Use Cases for Loose Equality

While generally discouraged, `==` has specific valid applications:

- **Null and undefined checks**: The expression `value == null` matches both `null` and `undefined` without matching other falsy values like `0` or `''`. This is more concise than `value === null || value === undefined`.
- **Legacy code maintenance**: When working with older codebases that rely on coercion behavior, consistency with existing patterns may require using `==`.
- **Specific algorithmic requirements**: Certain mathematical or sorting algorithms intentionally leverage JavaScript's coercion rules.

## Summary

- The `==` operator performs **type coercion** before comparison, while `===` requires identical **types and values**.
- Loose equality produces unpredictable results like `0 == false` and `'5' == 5` evaluating to `true`.
- Strict equality eliminates type conversion bugs and is the recommended default for modern JavaScript development.
- The h5bp/Front-end-Developer-Interview-Questions repository highlights this distinction in [`src/questions/javascript-questions.md`](https://github.com/h5bp/Front-end-Developer-Interview-Questions/blob/main/src/questions/javascript-questions.md) as a critical interview concept.
- Use `===` for all comparisons except specific cases like checking for both `null` and `undefined` with `== null`.

## Frequently Asked Questions

### Is === faster than == in JavaScript?

**Yes, strict equality (`===`) is generally faster** because it performs an immediate type check before comparing values. When types differ, `===` returns `false` immediately without executing coercion algorithms. Loose equality (`==`) must invoke JavaScript's complex Abstract Equality Comparison algorithm to convert types, adding computational overhead. However, the performance difference is negligible in most applications compared to the safety benefits of using `===`.

### Should I ever use == in modern JavaScript?

**You should avoid `==` in modern JavaScript except for one specific pattern.** The expression `value == null` is a concise, idiomatic way to check if a value is either `null` or `undefined` without catching other falsy values like `0`, `false`, or empty strings. This is equivalent to `value === null || value === undefined` but shorter. For all other comparisons, including comparing strings, numbers, booleans, and objects, always use `===` to prevent type coercion bugs.

### Why does null == undefined return true but null === undefined return false?

**This behavior stems from the ECMAScript specification's design of the Abstract Equality Comparison algorithm.** The specification explicitly states that `null` and `undefined` are equal when using loose equality (`==`) because both represent the absence of a meaningful value. However, strict equality (`===`) requires identical types, and since `null` is a distinct primitive type from `undefined`, they fail the type check immediately. This is intentional language design to allow `== null` checks to handle both missing value states while maintaining type safety with `===`.

### Where does the h5bp interview questions repository cover this topic?

**The h5bp/Front-end-Developer-Interview-Questions repository addresses this topic in [`src/questions/javascript-questions.md`](https://github.com/h5bp/Front-end-Developer-Interview-Questions/blob/main/src/questions/javascript-questions.md).** This file contains the canonical list of JavaScript interview questions used by employers worldwide, including the specific question about the difference between `==` and `===`. The repository treats this distinction as a fundamental concept that every frontend developer must understand, reflecting its importance in writing predictable, bug-free JavaScript code. You can find the specific question in the JavaScript section of the source file, which serves as a study guide for technical interviews.