Check if a String is Empty in JavaScript

By James L.

In this blog post, we will explore two approaches to check if a string is empty in JavaScript. We will discuss the following methods:

  1. Using the length property
  2. Using the strict equality operator (===)

1. Using the length property

The length property returns the length of the string. If it is equal to 0, it indicates the string is empty. Otherwise, it signifies the string contains one or more characters and is not empty.

For example:

let str = "";

if (str.length === 0) {
    console.log("String is empty"); 
}  else {
    console.log("String is not empty");
}

Output:

String is empty

A string containing only whitespace characters is not considered empty. However, if you want to treat a string with only whitespace characters as empty, you can use the trim() method to remove the leading and trailing whitespace characters before checking if the string is empty.

For example:

let str = " ";

if (str.trim().length === 0) {
    console.log("String is empty");
}

Output:

String is empty

2. Using the strict equality operator (===)

In this method, we compare the string to an empty string using the strict equality operator (===).

For example:

let str = "";

if (str === "") {
    console.log("String is empty");
} else {
    console.log("String is not empty");
}

Output:

String is empty

The strict equality operator (===) compares two values for equality and ensures that both the value and the data type are the same. 

For example, "3" === 3 returns false because the left operand is a string and the right operand is a number. However, "3" == 3  returns true because the equality operator (==) operator performs the type conversion before comparing the values.

If the string contains only whitespace characters then we can use the trim() method to remove the leading and trailing whitespace characters from the string before comparing it with an empty string.

For example:

let str = " ";

if (str.trim() === "") {
    console.log("String is empty");
} else {
    console.log("String is not empty");
}

Output:

String is empty

Things to be aware of

There are a few things to keep in mind while working with strings in JavaScript.

Initializing a string with an empty string

It is recommended to initialize a string variable with an empty string (e.g. let str = "") rather than just declaring it without initialization. If a variable is declared without being initialized, its value will be undefined. Initializing it with an empty string ensures that it starts with a valid string value. This way, we don’t have to check for undefined values before applying any string-related methods on the variable. 

For example:

// Initializing a string variable with an empty string
let str = "";

// Declaring a string variable without initializing it
// This can cause unexpected errors
let strTwo;
console.log(strTwo);  // Output: undefined

Resetting a string variable

When resetting a string variable, it is advised to assign it an empty string ("") rather than assigning it a null value. This practice helps avoid potential issues and errors that may arise from using a null value. This way, we don’t have to check for null values before applying any string-related methods on the variable. 

The null is a special value in JavaScript that represents the absence of any object value. By assigning it an empty string, you ensure that the variable remains a string type and can be safely used further in your code. 

For example:

let str = "Hello";

// Resetting a string variable
str = "";

// Avoid assigning a null to a string variable
// This can cause unexpected errors
str = null; 

In conclusion, initializing a string variable with an empty string and resetting it by assigning an empty string rather than a null are recommended to ensure consistent behavior and prevent potential issues when working with strings in JavaScript. By following these guidelines, we can avoid unexpected errors and ensure that the string variables maintain the expected data type throughout our code.