Difference between == and === in JavaScript
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
JavaScript has two equality operators: == (loose equality) and === (strict equality). The == operator compares values after type coercion, while === compares both value and type without any conversion. Understanding the difference is critical for avoiding subtle bugs.
Strict Equality (===)
=== returns true only if both the value and type are identical. No type conversion occurs:
Loose Equality (==)
== performs type coercion before comparing. JavaScript converts one or both operands to a common type:
Type Coercion Rules
When == compares different types, it follows these rules:
- Number vs String: The string is converted to a number
- Boolean vs Anything: The boolean is converted to a number (true→1, false→0)
- null vs undefined: They are equal to each other and nothing else
- Object vs Primitive: The object's
valueOf()ortoString()is called
Surprising Results with ==
The !== and != Operators
The same distinction applies to inequality:
Best Practice: Always Use ===
The === operator is predictable and avoids coercion bugs:
When == Is Acceptable
The only widely accepted use of == is checking for null or undefined:
ESLint Rules
Most JavaScript linters enforce strict equality:
The eqeqeq rule can be configured to allow == only for null checks:
Comparison Table
| Expression | == | === |
"5" ? 5 | true | false |
0 ? false | true | false |
"" ? false | true | false |
null ? undefined | true | false |
NaN ? NaN | false | false |
[1] ? 1 | true | false |
true ? 1 | true | false |
Common Pitfalls
- Truthy/falsy confusion:
==and truthy/falsy checks are different things."0" == falseistrue, but"0"is truthy in a boolean context (if ("0")is true). Do not conflate them. - NaN equality:
NaN === NaNisfalse. UseNumber.isNaN()to check for NaN, not===. - Object comparison: Both
==and===compare objects by reference, not value.{a:1} === {a:1}isfalsebecause they are different objects. - typeof check:
typeof null === "object"istrue(a known JavaScript quirk). Check for null explicitly before using typeof. - Automatic coercion in other contexts: Coercion also happens with
<,>,+, and other operators."5" + 3is"53"(string concatenation), not8.
Summary
===(strict equality) compares value and type — no coercion, no surprises==(loose equality) coerces types before comparing — can produce unexpected results- Always use
===and!==by default - The only acceptable use of
==isvalue == nullto check for both null and undefined - Enable the
eqeqeqESLint rule to enforce strict equality in your codebase
Related reading
- Difference between ( for... in ) and ( for... of ) statements?
- Difference between and
- Difference between Constructor and ngOnInit
- Difference between getAttribute() and getParameter()
- Difference between id and name attributes in HTML
- Difference between npx and npm?
- Difference between RegisterStartupScript and RegisterClientScriptBlock?
- Different latency time when measured through javascript vs developer console
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.