0
0
JavascriptHow-ToBeginner · 3 min read

How to Check if Variable is Empty in JavaScript

In JavaScript, you can check if a variable is empty by testing if it is null, undefined, an empty string "", or other empty values using if (!variable). For more specific checks, use conditions like variable === "" for empty strings or Array.isArray(variable) && variable.length === 0 for empty arrays.
📐

Syntax

Use simple conditional checks to test if a variable is empty. Common patterns include:

  • if (!variable): Checks if variable is falsy (null, undefined, 0, empty string, false, NaN).
  • variable === "": Checks if variable is exactly an empty string.
  • Array.isArray(variable) && variable.length === 0: Checks if variable is an empty array.
javascript
if (!variable) {
  // variable is empty or falsy
}

if (variable === "") {
  // variable is an empty string
}

if (Array.isArray(variable) && variable.length === 0) {
  // variable is an empty array
}
💻

Example

This example shows how to check different types of empty variables: undefined, null, empty string, and empty array.

javascript
const variables = [undefined, null, "", [], [1, 2], 0, false];

variables.forEach((variable, index) => {
  if (!variable) {
    console.log(`Variable at index ${index} is empty or falsy.`);
  } else {
    console.log(`Variable at index ${index} has a value.`);
  }
});
Output
Variable at index 0 is empty or falsy. Variable at index 1 is empty or falsy. Variable at index 2 is empty or falsy. Variable at index 3 is empty or falsy. Variable at index 4 has a value. Variable at index 5 is empty or falsy. Variable at index 6 is empty or falsy.
⚠️

Common Pitfalls

Using if (!variable) can mistakenly treat 0 or false as empty, which might not be what you want. Also, null and undefined are different but both falsy. For strings, check explicitly with variable === "" to avoid confusion.

javascript
const value = 0;

// Wrong: treats 0 as empty
if (!value) {
  console.log("Value is empty");
} else {
  console.log("Value is not empty");
}

// Right: check for null or undefined only
if (value === null || value === undefined) {
  console.log("Value is empty");
} else {
  console.log("Value is not empty");
}
Output
Value is empty Value is not empty
📊

Quick Reference

Check TypeHow to CheckDescription
Falsy valuesif (!variable)Catches null, undefined, 0, false, "", NaN
Empty stringvariable === ""Checks if variable is exactly an empty string
Empty arrayArray.isArray(variable) && variable.length === 0Checks if variable is an empty array
Null or undefinedvariable === null || variable === undefinedChecks only null or undefined values

Key Takeaways

Use if (!variable) to check for most empty or falsy values quickly.
Check empty strings explicitly with variable === "" to avoid confusion.
Empty arrays need length check with Array.isArray(variable) && variable.length === 0.
Be careful: 0 and false are falsy but may not mean empty in your context.
Use strict equality checks for precise empty value detection.