Checking for null, undefined, and empty values in JavaScript: a complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why checking for null, undefined, and emptiness is a pain for every JavaScript developer?

\n\n

Have you ever caught yourself thinking: "Why am I writing this bulky construct with if (x !== null && x !== undefined && x !== '') again?" If so — you are not alone. The question "Is there a standard function to check for null, undefined, or blank variables in JavaScript?" on Stack Overflow has gathered thousands of views. And this is no coincidence.

\n\n

JavaScript does not have a built-in function that checks everything at once. But that doesn't mean we are doomed to suffer. In this article, we will figure out why such a function doesn't exist, how to elegantly work around this limitation, and write our own perfect helper.

\n\n

Ready to turn chaos of checks into clean, understandable code? Let's go!

\n\n

What are null, undefined, and blank (empty string) in JavaScript?

\n\n

Before looking for a "magic pill", let's understand what we are dealing with. These three concepts are three different beasts, and confusing them is dangerous.

\n\n
    \n
  • undefined — a variable is declared but has no value assigned. This is the "default value" from JavaScript.
  • \n
  • null — a variable exists but is intentionally assigned "nothing". This is a conscious choice by the developer.
  • \n
  • blank (empty string) — '' or "". The string exists, but it contains no characters.
  • \n
\n\n

The key difference: null is an object (yes, this is a historical JS bug that is now a feature), undefined is a special type, and an empty string is a string.

\n\n
let a; // undefined\nlet b = null; // null\nlet c = ''; // empty string (blank)\nconsole.log(typeof a); // "undefined"\nconsole.log(typeof b); // "object" (yes, that's right!)\nconsole.log(typeof c); // "string"
\n\n

Now imagine: you want to check that a variable is "empty" in any sense. A regular check if (!value) will work for null and undefined, but will it not account for an empty string? No, it will account for it! Because an empty string is a falsy value. But there's a catch...

\n\n

Why is simply if (!value) a bad idea?

\n\n

Yes, if (!value) will catch null, undefined, '', 0, false, and NaN. But what if 0 or false are valid values for your variable? For example, if you are checking the number of items in a cart, 0 is normal, not "emptiness".

\n\n
function processOrder(itemCount) {\n    if (!itemCount) {\n        console.log('Error: no items!');\n    } else {\n        console.log('Processing order...');\n    }\n}\nprocessOrder(0); // Error: no items! — But this is not an error, it's 0 items
\n\n

This is why we need a specialized function, not a universal truthiness check.

\n\n

A standard function? No, but there are standard patterns

\n\n

To the question "Is there a standard function?" the answer is no. But JavaScript offers flexible tools so you can write your own. And this is even better, because your function will exactly match your business requirements.

\n\n

Option 1: Simplest check (strict)

\n\n

If you only need to check for null and undefined — use strict comparison (===). This is the safest way.

\n\n
function isNullOrUndefined(value) {\n    return value === null || value === undefined;\n}\n\nconsole.log(isNullOrUndefined(null)); // true\nconsole.log(isNullOrUndefined(undefined)); // true\nconsole.log(isNullOrUndefined('')); // false\nconsole.log(isNullOrUndefined(0)); // false
\n\n

Pros: does not confuse 0 and empty string with "nothing". Cons: does not check empty

Blogs

Book Recommendations