Home > Web Front-end > JS Tutorial > How to check for null/undefined/empty string in JavaScript?

How to check for null/undefined/empty string in JavaScript?

WBOY
Release: 2023-08-22 14:17:09
forward
1482 people have browsed it

How to check for null/undefined/empty string in JavaScript?

In JavaScript, "" represents an empty string, and we can use the null keyword to initialize the string to a null value. If we don't assign a value to any variable, it defaults to undefined.

Sometimes, when processing strings, we need to check whether the string is empty, undefined or has a null value. For example, we are getting details from the user through an HTML form, and the user can add an empty string in the input field; we need to validate the input field and display an error message to the user.

In this tutorial, we will learn three methods to check whether a string is empty, null or undefined.

Use string.trim() method and string.length property

The string.trim() method allows us to remove spaces from the beginning of the string. So we can remove the string from the beginning of the string. After that, we can check if the length of the string is zero, which means the string can be empty, null, or undefined.

grammar

Users can use the string.trim() method and string.length() property according to the following syntax to check for empty, undefined or null strings.

let string1 = " ";
string1 = string1.trim();
if (string1.length == 0) {
   // string is empty
}
Copy after login

Example

(example)

In the example below, we create two different strings. One is empty and the other contains only spaces. The user can see in the output that our logic shows that both strings are empty since the second string only contains spaces.

<html>
<body>
   <h3> Using the<i> string.trim() method and string.length() </i> property to check whether a string is null, undefined, or empty. </h3>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      let string1 = "";
      let string2 = " ";
      string1 = string1.trim();
      string2 = string2.trim();
      if (string1.length == 0) {
         output.innerHTML += "The string1 is empty! </br>";
      }
      if (string2.length == 0) {
         output.innerHTML += "The string2 is empty! </br>";
      }
   </script>
</body>
</html>
Copy after login

Convert a string to a boolean and check if it is empty, undefined or null

We can convert a string to a Boolean value using the Boolean constructor or the double negation operator (!!). When we convert any variable to boolean, it maps all false values ​​to false and other values ​​to true. In JavaScript, empty string, null and undefined are all false values, so when we convert it to a boolean value, the Boolean() constructor always returns false.

grammar

In the following syntax, we have used the Boolean() constructor to convert the string to a Boolean value and check if it is empty.

let string3 = undefined;
if (Boolean(string3) == false) {
   // string3 is either null, empty, or undefined
}
Copy after login

Example

(example)

We have converted three strings containing null, null and undefined values ​​in the example below. Additionally, we have created the isValid() function which takes a string as parameter and converts the string into a boolean value. We then check whether the value of the string returned from the Boolean() constructor is true or false.

<html>
<body>
   <h3> Converting the<i> string to boolean </i> to check whether a string is null, undefined, or empty. </h3>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      let str1 = "";
      let str2 = null;
      let str3 = undefined;
      function isValid(str) {
         if (Boolean(str)) {
         output.innerHTML += "The " + str + " is valid <br/>";
         } else {
            output.innerHTML += "The " + str + " is not valid <br/>";
         }
      }
      isValid(str1);
      isValid(str2);
      isValid(str3);
      isValid("Hello");
   </script>
</body>
</html>
Copy after login

In the above output, the user can observe that the Boolean constructor returns false for empty, null and undefined strings, and returns true for the string "Hello".

Use strict equality operator

The strict equality operator allows us to compare the values ​​of two variables and also compare the types of the variables. Here we will compare our string with "", null and undefined. Furthermore, we will use these three conditions together in a single if condition using the OR operator.

If any of these three conditions becomes true, it means that the string is invalid.

grammar

Users can use the strict equality operator according to the following syntax to check whether a string is empty, null or undefined.

if (str === "" || str === null || str === undefined) {
   // string is not valid
} else {
   // string is valid
}
Copy after login

Example

(example)

In the following example, the isValid() function contains an if-else statement to check whether the string is valid. As discussed in the syntax, we have used the OR operator in the condition of the if statement to check the three conditions of empty string, null and undefined string simultaneously.

<html>
<body>
   <h3> Using the<i> strict equality operator </i> to check whether string is null, undefined or empty.</h3>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      function isValid(str) {
         if (str === "" || str === null || str === undefined) {
         output.innerHTML += "The " + str + " is not valid <br/>";
          } else {
            output.innerHTML += "The " + str + " is valid <br/>";
         }
      }
      isValid(null);
      isValid(undefined);
      isValid("TutorialsPoint");
   </script>
</body>
</html>
Copy after login

Users learned three ways to check if a string is empty, undefined, or has a null value. From these three methods, the best method is the second method, which uses the Boolean() constructor.

However, users can use the Doble, Not(!!) operator, which provides simple syntax but is not suitable for beginners.

The above is the detailed content of How to check for null/undefined/empty string in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template