How to check empty string in JavaScript

In JavaScript, it is possible to verify if a string is empty or not through various techniques. Below are some illustrations:

1. Using the length property:

One way to determine if a string is empty is by checking its length property. When the length of a string is zero, it indicates that the string contains no characters.

For example:

Example

let str = "";

if (str.length === 0) {

console.log("String is empty");

} else {

console.log("String is not empty");

}

Output

Output

String is empty

2. Using the trim method:

The trim function eliminates leading and trailing spaces from a string. When applied to an empty string, trim will yield an empty string as a result.

Example

let str = "";

if (str.trim() === "") {

console.log("String is empty");

} else {

console.log("String is not empty");

}

Output

Output

String is empty

3. Using strict equality comparison with an empty string:

In JavaScript, an empty string is classified as falsy, signifying that it behaves as false in a Boolean scenario. Therefore, we can utilize strict equality comparison to verify if the string matches an empty string.

Example

let str = "";



if (str === "") {

console.log("String is empty");

} else {

console.log("String is not empty");

}

Output

Output

String is empty

4. Using ! operator:

In JavaScript, the ! operator can be utilized to verify if a string is falsy. It is important to note that an empty string is classified as falsy in JavaScript. Therefore, when the string is empty, the ! operator will evaluate to true.

Example

let str = "";



if (!str) {

console.log("String is empty");

} else {

console.log("String is not empty");

}

Output

Output

String is empty

5. Using charAt method:

The charAt function retrieves and returns the character located at a specific position within a string. If the string is devoid of any characters, the charAt method will yield an empty string.

Example

let str = "";



if (str.charAt(0) === "") {

console.log("String is empty");

} else {

console.log("String is not empty");

}

Output

Output

String is empty

6. Using regular expressions:

Regular expressions can also be utilized to validate an empty string. The regular expression below can be employed to match an empty string:

Example

let str = "";



if (/^\s*$/.test(str)) {

console.log("String is empty");

} else {

console.log("String is not empty");

}

Output

Output

String is empty

In the provided regular expression, the caret (^) symbol signifies the beginning of the string, \s* denotes zero or more occurrences of whitespace characters, and the dollar sign ($) represents the end of the string. When the string is either empty or consists solely of whitespace characters, the regular expression will successfully match and yield a true result.

7. Using the Object.prototype.toString method:

In situations where a variable can hold a string or a different type of object, you have the option to employ the Object.prototype.toString method to ascertain its type. Subsequently, you can validate whether it is a string and whether it is devoid of any content.

Example

let str = {};



if (Object.prototype.toString.call(str) === "[object String]" &&str.trim() === "") {

console.log("String is empty");

} else {

console.log("String is not empty");

}

Output

Output

String is empty

In this script, the type of the variable str is determined using the Object.prototype.toString function. Subsequently, the script verifies if the type is a string by comparing the outcome to the string "[object String]". If the variable is indeed a string, it proceeds to strip any whitespace and confirms if the string is vacant.

8. Using the toString method:

When dealing with a variable that may hold a string, null, or undefined value, it is possible to utilize the toString method to transform it into a string and subsequently verify if it is empty.

Example

let str = null;



if (str &&str.toString().trim() === "") {

console.log("String is empty");

} else {

console.log("String is not empty");

}

Output

Output

String is empty

The following code snippet verifies the presence of the variable str by checking if it is neither null nor undefined. Subsequently, the code converts the variable to a string utilizing the toString function. Following this conversion, the code trims the resultant string and validates whether it is devoid of content.

9. Using the reduce method:

By utilizing the reduce method, we can efficiently iterate through an array of strings to verify if any of them are empty strings. This process involves systematically checking each string within the array for emptiness.

Example

let arr = ["", "hello", "world"];



if (arr.reduce((acc, val) =>acc || val.trim() === "", false)) {

console.log("Array contains an empty string");

} else {

console.log("Array does not contain an empty string");

}

Output

Output

Array contains an empty string

Utilizing the reduce function, this script iterates through the array named arr to verify the presence of any empty strings within it. The reduce function requires a callback function, which is invoked for every element in the array, alongside an initial value set to false. Within the callback function, each element is examined to determine if it is an empty string. If an empty string is encountered, true is returned; otherwise, the accumulator value (referred to as acc) is returned. Should the array include any empty strings, the reduce function will yield a true outcome, signaling the existence of an empty string within the array.

Input Required

This code uses input(). Please provide values below: