Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to check if a Variable Is Not Null in JavaScript ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

You can easily check if a variable Is Null or Not Null in JavaScript by applying a simple if-else condition to the given variable. 

There are two ways to check if a variable is null or not. First I will discuss the wrong way that seems to be right to you at first, then we will discuss the correct way to check if a variable is null or not.

Method 1: The wrong way seems to be right.

Condition:

if(my_var) {
    ....
}

Note: When a variable is null, there is an absence of any object value in a variable. Null is often retrieved in a place where an object can be expected, but no object is relevant.

By the above condition, if my_var is null then the given if condition will not execute because null is a falsy value in JavaScript, but in JavaScript, there are many pre-defined falsy values like

  • undefined
  • null
  • 0
  • “” ( empty string)
  • false
  • NaN

So if my_var is equal to any of the above pre-defined falsy values then if the condition would not execute or vice-versa. 

Example: This example shows the use of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        How to check if a Variable Is Not Null in JavaScript ?
    </title>
</head>
 
<body>
    <h2 style="color:green;">
        GeeksforGeeks
    </h2>
    <p>
        variable-name : GFG_Var
    </p>
    <button onclick="myGeeks()">
        Check for vowel
    </button>
 
    <h3 id="div" style="color:green;">HTML</h3>
 
    <!-- Script to check existence of variable -->
    <script>
        function myGeeks() {
 
            var h3 = document.getElementById("div");
 
            var GFG_Var = h3.innerHTML;
 
            // check if GFG_Var variable contain any vowels
            // HTML text contains no vowels,
            // so variable my_var will be assigned null
            const my_var = GFG_Var.match(/[aeiou]/gi);
 
            if (my_var) {
                h3.innerHTML = "Variable is not null";
            }
            else {
                h3.innerHTML = "Variable is NULL";
            }
        }
    </script>
</body>
</html>


Output:

Check for vowel

Method 2:  The following code shows the correct way to check if the variable is null or not.

Condition:

if(my_var !== null)
{
    ....
}

The above condition is actually the correct way to check if a variable is null or not. The if condition will execute if my_var is any value other than a null i.e 

  • If my_var is undefined then the condition will execute.
  • If my_var is 0 then the condition will execute.
  • If my_var is (empty string) then the condition will execute.

This condition will check the exact value of the variable whether it is null or not.

Example: This example shows the use of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        How to check if a Variable Is Not Null in JavaScript ?
    </title>
</head>
 
<body>
    <h2 style="color:green;">
        GeeksforGeeks
    </h2>
 
 
    <p>
        variable-name : GFG_Var
    </p>
 
 
    <button onclick="myGeeks()">
        Check for vowel
    </button>
 
    <h3 id="div" style="color:green;">HTML</h3>
 
    <!-- Script to check existence of variable -->
    <script>
        function myGeeks() {
 
            var h3 = document.getElementById("div");
 
            var GFG_Var = h3.innerHTML;
 
            // check if GFG_Var variable contain any vowels
            // HTML text contain no vowels
            // so variable my_var will assign null
            const my_var = GFG_Var.match(/[aeiou]/gi);
 
            // this will check exactly whether variable is null or not
            if (my_var !== null) {
                h3.innerHTML = "Variable is not null";
            }
            else {
                h3.innerHTML = "Variable is NULL";
            }
        }
    </script>
</body>
</html>


Output: The output is the same as in the first example but with the proper condition in the JavaScript code.


My Personal Notes arrow_drop_up
Last Updated : 22 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials