How to Check If String is Empty in JavaScript

Programmers deal with a lot of data types. They are string, boolean, number, integers, objects, etc. The string data type is among the most widely used data types. The string data type is a sequence of characters enclosed within ” ” of ‘ ‘.Sometimes although the string is defined, the programmer keeps it empty, i.e., with no content.

In this article, we will learn how to find to check if a given string is empty.

Check If String is Empty in JavaScript

To check if a string is empty in JavaScript, use the comparison (==) operator. To check if the string is equal to “” and the condition evaluates to true, the string is empty; otherwise, it is not.

Example 1

let a = "";

if (a == "") {

 console.log("Yes this is an empty string");

}

Output

Yes this is an empty string

Explanation

Although we used the let keyword to create the string, we have not assigned any character to it. Instead, we only passed zero-length characters. Hence the condition evaluates to true, and javascript outputs the message in the console.

Example 2

let a = "";

if (a == 0) {

 console.log("Yes this is an empty string");
}

Output

Yes this is an empty string

Explanation

As we know, the == changes the variable’s data type to some standard value; hence the empty string is treated as if it is equivalent to 0(zero). Hence the condition is evaluated as true, and javascript prints the message in the console.

Using === Operator

It can be described as a strict equality operator. It does not change the data type of the variable to be checked. For example, if we evaluate the condition and check if it is equal to “” through the === operator, it will return true if it is an empty string.

Example 1

let a="";

if(a===””){

    console.log("Yes this is an empty string");

}

Output

Yes this is an empty string

Explanation

In the above code, since the string is empty, the javascript condition returns true; hence, the message got printed in the console.

Example 2

let a = "";

if (a === 0) {

 console.log("Yes this is empty string");

}

else {

 console.log("No this is not empty string")

}

Output

No this is not the empty string

Explanation

In the above code, we have used the === operator, which does not change the data types of the string. Hence empty string is not treated as equal to 0. So the condition is evaluated as false, and the second message is printed in the console.

Using length function 

We can also check for the length of the string. The length of the string is zero (0), which means that the string is empty.

Example

let a = "";

if (a.length == 0) {

 console.log("Yes this is an empty string");

}

else {

 console.log("No this is not empty string")

}

Output

Yes this is an empty string

Explanation

We created a string named a using the let keyword. This is an empty string because it does not contain any character. So the length of the string is equal to 0. The condition evaluates to true.

Conclusion

This article taught us how to check if a string is empty in JavaScript. Many methods can be used here which are using == or === operator etc. Mostly these techniques are used for debugging/removing errors in the program if there are any related to string. That’s it for this tutorial.

Related posts

How to Check If Object is Empty in JavaScript

How to Check If Variable Exists in Javascript

JavaScript empty array

Leave a Comment