Signup/Sign In

How to check for an empty string in JavaScript?

Answer: Using strict equality (===) operator and the length property

In this lesson, we are going to discuss the methods to check whether the string is empty or not.

The need for checking whether the string is empty or not arises when we validate the data in JavaScript then we have to ensure that a variable (string) must contain a valid value.

There are various ways to check whether the string is empty or not, which are given below:

  • Using strict equality operator
  • Using length property

Using strict equality (===) operator

We can check whether the string is empty or not using the strict equality (===) operator. This operator returns true when the string is not empty otherwise it returns false.

Using this operator, we will get true only when the data type of the value is a string and the value is not empty. If any one of the given conditions is not fulfilled then we will get false.

Example: Using strict equality (===) operator

In the given example, we have created a variable empStr and specified its value an empty string (""). Then, within the if statement we have compared the value of the empStr variable using the strict equality (===) operator.

<!DOCTYPE html>
<html lang="en">
<head>
	<title>Check for an empty string</title>
</head>
	<script>
		let empStr = "";
		if (empStr === "") {
			console.log("String is empty");
		} else {
			console.log("String is not empty");
		}
	</script>
</body>
</html>

Output


String is empty

Using length property

Another way to check whether the string is empty or not is using the JavaScript length property. We will have to compare the string length with 0 (zero) using strict equality (===) operator. If it returns true then the string is empty otherwise string is not empty.

Do not use the equality operator instead of the strict equality operator while comparing the string value because the equality operator returns true if the value is 0.

Example: Using length property

In the given example, we have used the length property to check whether the given string is empty or not.

<!DOCTYPE html>
<html>
<head>
  <title>Check for an Empty String</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <script type="text/javascript">
    var myString1 = "";
    console.log(myString1.length === 0);
    var myString2 = "Hello";
    console.log(myString2.length === 0);
    var myString3 = 1;
    console.log(myString3.length === 0);
  </script>
</body>
</html>

Output


true
false
false

Conclusion

In this lesson, we have learned how to check whether the string is empty or not. We have discussed two methods to do this. In the first method, we have used the strict equality (===) operator. This operator compares the value of the string and then returns true or false based on the comparison. While in the second method we have used the length property, We have compared the length of the string with 0 (zero). If the length of the string is zero then it will return true otherwise it returns false.



About the author: