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

10 May, 2021
You can easily check if a variable Is Null or Not Null in JavaScript by applying 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 that 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 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 condition would not execute or vice-versa. 
Example 1:
HTML
<!DOCTYPE html><html> <body style="text-align:center;">          <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 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 2:
HTML
<!DOCTYPE html><html><body style="text-align:center;">          <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.
javascript-basics
JavaScript-Questions
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript