Last modified: Jul 22, 2026

JavaScript Check if Variable is Empty

Checking if a variable is empty is a common task in JavaScript. An empty variable can be null, undefined, an empty string, an empty array, or an empty object. Each type needs a different check. This guide shows you how to handle them all.

You will learn simple methods to test for emptiness. We use clear examples and code snippets. This article is perfect for beginners. It helps you avoid errors in your code.

What Does Empty Mean in JavaScript?

In JavaScript, "empty" is not a single value. It depends on the variable type. A string with no characters is empty. An array with no items is empty. An object with no properties is also empty. Always check the type first before testing for emptiness.

Common empty values include null, undefined, "" (empty string), 0 (zero), and false. But zero and false are not always considered empty. You must decide based on your needs.

Check for Null or Undefined

The simplest check is for null or undefined. Use a strict equality operator. This works for any variable that might not have a value.

 
let value1 = null;
let value2;
// Check if null or undefined
if (value1 === null || value1 === undefined) {
  console.log("value1 is empty");
}
if (value2 === null || value2 === undefined) {
  console.log("value2 is empty");
}

value1 is empty
value2 is empty

You can also use the == operator. It checks both null and undefined at once. But use strict equality (===) for clarity. It avoids unexpected results.

Check for Empty String

An empty string has a length of zero. Use the .length property to check. This is fast and reliable.

 
let str = "";
if (str.length === 0) {
  console.log("String is empty");
}
// Alternative: trim whitespace
let str2 = "   ";
if (str2.trim().length === 0) {
  console.log("String is empty after trimming");
}

String is empty
String is empty after trimming

Remember to trim whitespace if needed. A string with only spaces looks empty. Use trim() to remove extra spaces before checking.

Check for Empty Array

An empty array has no elements. Use the .length property again. This works for all array types.

 
let arr = [];
if (Array.isArray(arr) && arr.length === 0) {
  console.log("Array is empty");
}

Array is empty

Always verify the variable is an array first. Use Array.isArray() to avoid errors. Then check the length. This method is safe and clear.

Check for Empty Object

An empty object has no own properties. Use Object.keys() to get an array of keys. Then check its length.

 
let obj = {};
if (Object.keys(obj).length === 0) {
  console.log("Object is empty");
}

Object is empty

This checks only own properties. It ignores inherited properties. This is the standard way to test object emptiness. It works in all modern browsers.

Check for Any Empty Value

You may need a single function for all types. Write a helper that handles different cases. Use typeof and .length where needed.

 
function isEmpty(value) {
  // Check null or undefined
  if (value === null || value === undefined) {
    return true;
  }
  // Check string or array
  if (typeof value === "string" || Array.isArray(value)) {
    return value.length === 0;
  }
  // Check object
  if (typeof value === "object") {
    return Object.keys(value).length === 0;
  }
  // Default: not empty (e.g., numbers, booleans)
  return false;
}
console.log(isEmpty(null));        // true
console.log(isEmpty(""));          // true
console.log(isEmpty([]));          // true
console.log(isEmpty({}));          // true
console.log(isEmpty(0));           // false

true
true
true
true
false

This function works for most cases. You can extend it for your needs. For example, treat 0 as empty if your app requires it.

Common Pitfalls

Avoid using if (variable) alone. It treats 0 and false as falsy. This may not match your definition of empty. Always be explicit about what empty means.

Another mistake is checking an object with obj === {}. This does not work. Objects are compared by reference, not by content. Use Object.keys() instead.

For more on variable handling, see our JavaScript Variable Interpolation Guide. It explains how to work with variables in strings.

Best Practices

Write reusable functions for emptiness checks. This keeps your code clean. Use Array.isArray() before checking array length. Use typeof to identify strings.

Test your code with different values. Include null, undefined, empty strings, arrays, and objects. This prevents bugs in production.

Learn more about variable types in our JavaScript Variable Types Guide. It covers all data types in detail.

Conclusion

Checking if a variable is empty in JavaScript requires type-specific logic. Use === null for null, .length for strings and arrays, and Object.keys() for objects. Write a helper function to simplify your code. Always be explicit about your definition of empty. This approach makes your code robust and easy to read.

For more examples, see our JavaScript Variables Examples page. It shows practical use cases for variable checks.