You can use the length property of a string in JavaScript to check if it is empty. The length property returns the number of characters in a string.

For example:

let str = "";
if (str.length === 0) {
  console.log("The string is empty");
}

Alternatively, you can use the !str (not str) to check if the string is empty or not.

let str = "";
if (!str) {
  console.log("The string is empty");
}

Another way is to check the string against an empty string using the === operator.

let str = "";
if (str === "") {
  console.log("The string is empty");
}

You can also check this by using trim() method to remove any whitespace from both ends of a string and check if it is empty or not

let str = "   ";
if (!str.trim()) {
  console.log("The string is empty");
}

These methods will return true if the string is empty, and false if it is not.

Also Read:

Categorized in: