How do I check for null values in JavaScript?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking for null in JavaScript looks simple, but it becomes confusing once undefined, falsy values, and loose equality enter the picture. The right check depends on whether you mean exactly null or whether you want to treat both null and undefined as "missing."
Check for Exactly null
If you only want the literal null value, use strict equality. It does not perform type coercion, so it distinguishes null from everything else.
This is the safest and clearest choice when your code needs to behave differently for null than for undefined, 0, false, or an empty string.
Strict equality is what you want in codebases where API contracts matter. If a function promises to return null to mean "known absence," checking with === null communicates that rule clearly.
Check for Either null or undefined
Sometimes the business rule is broader: any missing value should be handled the same way. In that case, the loose equality check value == null is one of the few places where == is intentionally useful, because it matches both null and undefined, but not other falsy values.
That expression is compact and readable once you know the rule. The important part is to use it deliberately, not by accident.
Do Not Confuse null with Falsy
A check like if (!value) does not mean "is null." It means the value is falsy, which includes 0, false, NaN, "", null, and undefined.
This is useful in some UI logic, but it is the wrong tool when you must distinguish between a deliberately empty value and a missing one. For example, a quantity of 0 is often valid data, not an absence marker.
Another point that surprises people is typeof null === "object". That is a long-standing language quirk, not a sign that null should be treated like a real object. Do not use typeof if the goal is a direct null check.
Optional Chaining and Nullish Coalescing
Modern JavaScript gives you tools that reduce manual null checks in property access and defaults.
Optional chaining stops property access when an intermediate value is null or undefined, and nullish coalescing provides a fallback only for those two missing-value cases. That makes the code shorter without collapsing valid falsy values like 0 or "".
These operators do not replace every explicit check, but they eliminate a lot of boilerplate in code that walks nested data from APIs or form state.
Common Pitfalls
- Using
!valuewhen you really need to detect onlynull. - Forgetting that
value == nullmatches bothnullandundefined. - Treating
0,false, or""as missing because they are falsy. - Relying on
typeof null, which returns"object"for historical reasons. - Adding unnecessary utility helpers when a direct equality check is clearer.
Summary
- Use
value === nullwhen you mean exactlynull. - Use
value == nullonly when you intentionally wantnullorundefined. - Do not use
!valueas a substitute for a real null check. - Optional chaining and nullish coalescing make missing-value handling cleaner.
- Distinguishing null, undefined, and falsy values avoids subtle bugs.

