Checking for Empty String, null and undefined 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

Introduction: Why This Task Is Simpler Than It Seems

\n\n

Every developer who has written JavaScript at least once has encountered a situation where they need to check if a string is empty, or if it might be null or undefined. It seems simple, right? But let's be honest: how many times have you written if (str == "") and got a bug when the variable turned out to be null? And how many times have you tried to call .trim() on undefined and caught a red error in the console?

\n\n

This topic is one of the most popular questions on Stack Overflow with tens of thousands of views. And for good reason. JavaScript has many nuances that turn a simple check into a minefield. In this article, we will break down all possible methods, from the most obvious to the professional ones, and figure out which one is best to use in your project.

\n\n

We will talk about what falsy values are, how the == and === operators work, and why "" and 0 are not the same as false. By the end of the article, you will know exactly how to write a universal check that won't let you down.

\n\n

The Problem: What Counts as an "Empty" String?

\n\n

Before writing code, we need to clearly define what we are checking. In the context of the question "How do I check for an empty/undefined/null string in JavaScript?", we usually mean three states:

\n\n
    \n
  • Empty string ("" or '') — a string of zero length.
  • \n
  • null — the intentional absence of a value.
  • \n
  • undefined — a variable is declared but not initialized.
  • \n
\n\n

But there is also a fourth, implicit case: a string consisting only of spaces (" "). It is often also considered "empty" from a business logic perspective. We will cover all the options.

\n\n

Method 1: Logical Conversion (The Simplest)

\n\n

The most popular and concise way is to use the fact that JavaScript automatically converts values to true or false in a boolean context.

\n\n
let str = "";\n\nif (!str) {\n    console.log("String is empty, null or undefined");\n} else {\n    console.log("String is not empty");\n}\n
\n\n

This code will work for "", null, undefined, 0, NaN, and false. Why? Because all these values are considered falsy in JavaScript. If you are sure that the variable is a string (or could be null/undefined), then this method is sufficient.

\n\n

But there is a catch: if the variable equals 0 (a number), the check will also say "empty". If this is critical, this method is not suitable.

\n\n

Method 2: Strict Comparison (Explicit Control)

\n\n

If you want to be as precise as possible and check only the three specified states, use strict comparison (===).

\n\n
function isEmptyOrNull(str) {\n    return str === "" || str === null || str === undefined;\n}\n\nconsole.log(isEmptyOrNull(""));      // true\nconsole.log(isEmptyOrNull(null));    // true\nconsole.log(isEmptyOrNull(undefined)); // true\nconsole.log(isEmptyOrNull("hello")); // false\nconsole.log(isEmptyOrNull(0));       // false (number won't pass)\n
\n\n

This code works without surprises. It does not confuse 0 or false with an empty string. But it does not handle strings with spaces.

\n\n

Method 3: Trim + Check (For "Real" Emptiness)

\n\n

Often users enter spaces into a form, and the string " " visually looks empty. To catch such cases as well, you need to trim the spaces using the .trim() method.

\n\n
function isBlank(str) {\n    return !str || str.trim() === "";\n}\n\nconsole                

Blogs

Book Recommendations