Oct 2026 · Guide · ~7 min read
Why === Beats == in JavaScript — Always
By Safdar Ali — frontend engineer, Bengaluru
Loose equality (==) converts values before it compares them. Strict equality (===) compares type and value. I use strict equality in every new file. The conversion, if I want it, is a line I can read.
If you have not read the companion piece, start with type coercion. Equality bugs are coercion bugs wearing a comparison operator.
Examples that fail review
selectedId == product.id
// selectedId came from a query string: "42"
// product.id came from the database: 42
// == is true. A later .includes or Map lookup with === is false.
status == 0
// status is "0" from a checkbox value attribute
// == is true, so "pending" and "done" logic both misfire
user.role == false
// never means what the author thoughtThe fix is boring. Convert once, then compare strictly.
const selectedId = Number(searchParams.get("id"));
if (!Number.isFinite(selectedId)) return notFound();
const active = products.find((p) => p.id === selectedId);The null and undefined exception people quote
null == undefined is true. Some codebases still write value == null to catch both. I do not. Two explicit checks are easier to search:
function isMissing(value) {
return value === null || value === undefined;
}TypeScript's value == null narrows both, which is why the shortcut survives in typed code. If your team uses it, keep it in one helper so the rest of the repo stays on ===.
The rule
- Compare with === and !==.
- Convert types in a named step before the comparison.
- Reject NaN with Number.isFinite, not with ==.
- Do not "fix" a failing === by switching to ==. The types disagree for a reason.
If this helped you
I publish free tutorials and write-ups like this in my spare time — no paywall on the guides. If it saved you an afternoon of trial and error, you can support the work:
- Buy me a coffee at buymeacoffee.com/safdarali
- Subscribe to my YouTube channel — it's free; 70+ React & Next.js tutorials
Related reading
More guides on safdarali.in — same author, production-focused.
- Guide
How to Build a Frontend Developer Portfolio That Stands Out
Frontend developer portfolio guide for India — sections, React/Next.js examples, SEO, performance, personal branding, FAQ, and checklist to build and rank.
May 2026Read article →
- Guide
React Server Components vs Client Components — When to Use Which
Practical RSC vs client guide for Next.js App Router — when to use each, real code, bundle before/after, and performance impact.
May 2026Read article →