Introduction: Why This Task Is Simpler Than It Seems
\n\nEvery 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?
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\nWe 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.
The Problem: What Counts as an "Empty" String?
\n\nBefore 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
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.
Method 1: Logical Conversion (The Simplest)
\n\nThe most popular and concise way is to use the fact that JavaScript automatically converts values to true or false in a boolean context.
\n\nlet 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\nThis 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.
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.
Method 2: Strict Comparison (Explicit Control)
\n\nIf you want to be as precise as possible and check only the three specified states, use strict comparison (===).
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\nThis code works without surprises. It does not confuse 0 or false with an empty string. But it does not handle strings with spaces.
Method 3: Trim + Check (For "Real" Emptiness)
\n\nOften 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.
function isBlank(str) {\n return !str || str.trim() === "";\n}\n\nconsole