How to check empty string in JavaScript

In JavaScript, we can check whether a string is empty or not using several methods. Here are a few examples:

1. Using the length property:

We can use the length property of a string to check whether it has any characters or not. If the length is zero, it means the string is empty.

For example:

Output

String is empty

2. Using the trim() method:

The trim() method removes whitespace from both ends of a string. If the string is empty, trim() will return an empty string.

Output

String is empty

3. Using strict equality comparison with an empty string:

In JavaScript, an empty string is considered falsy, which means it is treated as false in a Boolean context. So, we can use strict equality comparison to check if the string is equal to an empty string.

Output

String is empty

4. Using ! operator:

We can use the ! operator to check if the string is falsy. An empty string is considered falsy in JavaScript, so if the string is empty, ! will return true.

Output

String is empty

5. Using charAt() method:

The charAt() method returns the character at a specified index in a string. If the string is empty, charAt() will return an empty string.

Output

String is empty

6. Using regular expressions:

We can also use regular expressions to check for an empty string. The following regular expression matches an empty string:

Output

String is empty

In this regular expression, ^ matches the start of the string, \s* matches zero or more whitespace characters, and $ matches the end of the string. If the string is empty or contains only whitespace characters, the regular expression will match and return true.

7. Using the Object.prototype.toString() method:

If you have a variable that could be a string or another type of object, you can use the Object.prototype.toString() method to get its type and then check if it is a string and if it is empty.

Output

String is empty

This code uses the Object.prototype.toString() method to get the type of the variable str, and then checks if it is a string by comparing the result to the string "[object String]". If it is a string, it trims the string and checks if it is empty.

8. Using the toString() method:

If we have a variable that could be a string or null or undefined, we can use the toString() method to convert it to a string and then check if it is empty.

Output

String is empty

This code first checks if the variable str is not null or undefined, and then converts it to a string using the toString() method. After that, it trims the resulting string and checks if it is empty.

9. Using the reduce() method:

If we have an array of strings and we want to check if any of them are empty, we can use the reduce() method to iterate over the array and check if any of the strings are empty.

Output

Array contains an empty string 

This code uses the reduce() method to iterate over the array arr and check if any of the strings in the array are empty. The reduce() method takes a callback function that is called on each element of the array, and a starting value of false. The callback function checks if the current element is an empty string and returns true if it is, or the accumulator value (acc) otherwise. If any element in the array is an empty string, the final result of the reduce() method will be true, indicating that the array contains an empty string.