JavaScript
Null Values
Web Development
Coding Tutorials
Programming Tips

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.

javascript
1const value = null;
2
3if (value === null) {
4  console.log("value is exactly null");
5}

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.

javascript
1function printName(user) {
2  if (user.name == null) {
3    console.log("name is missing");
4    return;
5  }
6
7  console.log(user.name);
8}
9
10printName({ name: null });
11printName({});

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.

javascript
1const samples = [null, undefined, 0, false, ""];
2
3for (const item of samples) {
4  if (!item) {
5    console.log("falsy:", item);
6  }
7}

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.

javascript
const city = user?.address?.city ?? "Unknown city";

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 !value when you really need to detect only null.
  • Forgetting that value == null matches both null and undefined.
  • 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 === null when you mean exactly null.
  • Use value == null only when you intentionally want null or undefined.
  • Do not use !value as 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.

Course illustration
Course illustration

All Rights Reserved.