Question
JavaScript == vs ===: Which Equality Operator Should You Use?
Question
I'm using JSLint on some JavaScript code, and it suggests replacing == with === in comparisons such as this:
if (idSele_UNVEHtype.value.length == 0) {
// ...
}
Should === be preferred over == in JavaScript comparisons?
I want to understand:
- Is there any performance benefit to using
===instead of==? - Why do linters usually recommend
===? - If no type conversion happens, is
===still better than==?
Short Answer
By the end of this page, you'll understand the difference between JavaScript's loose equality operator (==) and strict equality operator (===). You'll learn how type coercion affects comparisons, why === is usually the safer default, whether performance matters, and how real codebases handle equality checks.
Concept
In JavaScript, both == and === compare two values, but they do it differently.
==checks for equality after possible type conversion.===checks for equality without type conversion.
That difference is the main reason developers strongly prefer ===.
Loose equality: ==
When you use ==, JavaScript may try to convert one or both values to a common type before comparing them.
Examples:
0 == "0" // true
false == 0 // true
"" == 0 // true
null == undefined // true
These results can be surprising, especially for beginners.
Strict equality: ===
When you use ===, both the and the must match.
Mental Model
Think of == and === like two different security checks.
==is a guard who says, "You don't look exactly right, but I'll convert your badge and see if it matches."===is a guard who says, "Your badge and identity must already match exactly."
With ==, JavaScript may "adjust" values before deciding.
With ===, JavaScript checks them as they are.
That is why === is easier to trust: no hidden conversion step happens first.
Syntax and Examples
The basic syntax looks the same, but the behavior differs.
value1 == value2 // loose equality
value1 === value2 // strict equality
Example 1: Number and string
console.log(5 == "5"); // true
console.log(5 === "5"); // false
Explanation:
==converts the string"5"to the number5===does not convert types, so number and string are not equal
Example 2: Form input values
HTML form input values are usually strings.
const age = "18";
console.log(age == 18); // true
console.log(age === );
Step by Step Execution
Consider this code:
const inputValue = "0";
console.log(inputValue == 0);
console.log(inputValue === 0);
Step 1: Evaluate inputValue == 0
Left side:
"0"
Right side:
0
The types are different:
- left is a string
- right is a number
Because == allows coercion, JavaScript converts the string "0" to the number 0.
Now it compares:
0 == 0
Result:
true
Real World Use Cases
Form validation
Form fields usually return strings.
const quantity = input.value;
if (Number(quantity) === 0) {
console.log("Quantity cannot be zero");
}
API response handling
APIs may return numeric-looking values as strings.
if (response.status === 200) {
console.log("Success");
}
If the API returns "200" instead, you should convert it deliberately instead of relying on ==.
Filtering data
const users = [
{ active: true },
{ active: false }
];
const activeUsers = users.filter(user => user.active === true);
Real Codebase Usage
In real codebases, === is the default choice because it reduces ambiguity.
Common patterns
Guard clauses
function greet(name) {
if (name === "") {
return "Name is required";
}
return `Hello, ${name}`;
}
Validation before processing
function setCount(count) {
if (typeof count !== "number") {
throw new Error("count must be a number");
}
if (count === 0) {
return "Nothing to process";
}
}
Explicit conversion before comparison
const page = Number(query.page);
if (page === 1) {
.();
}
Common Mistakes
Mistake 1: Assuming == compares values only
console.log(false == 0); // true
Why it happens:
falseis coerced to0
How to avoid it:
- use
=== - convert types yourself before comparing
Mistake 2: Comparing form input directly to numbers
Broken example:
const age = document.querySelector("#age").value;
if (age === 18) {
console.log("Adult");
}
This fails because age is a string.
Better:
const age = Number(.().);
(age === ) {
.();
}
Comparisons
| Operator | Name | Type conversion? | Example | Result |
|---|---|---|---|---|
== | Loose equality | Yes, if needed | 5 == "5" | true |
=== | Strict equality | No | 5 === "5" | false |
!= | Loose inequality | Yes, if needed | 5 != "5" | false |
Cheat Sheet
// Preferred default
if (a === b) {}
if (a !== b) {}
// Loose equality allows coercion
5 == "5" // true
false == 0 // true
"" == 0 // true
null == undefined // true
// Strict equality does not coerce
5 === "5" // false
false === 0 // false
"" === 0 // false
null === undefined // false
Rules of thumb
- Prefer
===and!== - Convert values explicitly before comparing
- Be careful with form values because they are usually strings
- Do not choose
==for performance reasons - A common intentional exception is:
if (value == ) {
}
FAQ
Is === faster than == in JavaScript?
Usually, the performance difference is negligible. Choose === for correctness and clarity, not speed.
Why do linters recommend ===?
Because it avoids unexpected type coercion and makes comparisons easier to reason about.
Should I always use ===?
Almost always, yes. One common exception is value == null when you intentionally want to match both null and undefined.
What is type coercion in JavaScript?
Type coercion is when JavaScript automatically converts a value from one type to another, such as turning "5" into 5 during a == comparison.
Why does "0" == 0 return true?
Because == converts the string "0" to the number 0 before comparing.
Why does return false?
Mini Project
Description
Build a small input-checking script that compares user-provided values safely. This project demonstrates why explicit conversion plus strict equality is more reliable than loose equality when handling form-like input.
Goal
Create a script that validates a few string inputs and compares them correctly using ===.
Requirements
- Create variables that simulate user input as strings.
- Convert numeric input to a number before comparing it.
- Use
===for all main comparisons. - Show at least one example where
==would give a misleading result. - Print clear messages to the console for each check.
Keep learning
Related questions
Deep Cloning Objects in JavaScript: Methods, Trade-offs, and Best Practices
Learn how to deep clone objects in JavaScript, compare structuredClone, JSON methods, and recursive approaches with examples.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers with clear examples.
How JavaScript Closures Work: A Beginner-Friendly Guide
Learn how JavaScript closures work with simple explanations, examples, common mistakes, and practical use cases for real code.