JavaScript | Check the existence of variable

30 Aug, 2021
JavaScript has a built-in function to check whether a variable is defined/initialized or undefined.Note:  
The typeof operator will check whether a variable is defined or not.
The typeof operator doesn’t throw a ReferenceError exception when it is used with an undeclared variable.
The typeof null will return an object. So, check for null also.
Example 1: This example checks whether a variable is defined or not.  
html
<!DOCTYPE html><html> <head>    <title>        JavaScript to check existence of variable    </title></head>         <body style = "text-align:center;">         <h1 style = "color:green;" >        GeeksForGeeks    </h1>                  <p>        variable-name : GFG_Var    </p>      <button onclick="myGeeks()">        Check    </button>         <h3 id = "div" style="color:green;"></h3>                 <!-- Script to check existence of variable -->    <script>        function myGeeks() {                         var GFG_Var;            var h3 = document.getElementById("div");                         if (typeof GFG_Var === 'undefined') {                h3.innerHTML = "Variable is Undefined";            }            else {                h3.innerHTML = "Variable is defined and"                    + " value is " + GFG_Var;            }        }    </script></body> </html>                   
Output:  
Before clicking the button:  
After clicking the button:  
Example 2: This example also checks whether a variable is defined or not.  
html
<!DOCTYPE html><html> <head>    <title>        JavaScript to check existence of variable    </title></head>         <body style = "text-align:center;">         <h1 style = "color:green;" >        GeeksForGeeks    </h1>                  <p>        variable-name : GFG_Var    </p>      <button onclick="myGeeks()">        Check    </button>         <h3 id = "div" style="color:green;"></h3>                 <!-- Script to check existence of variable -->    <script>        function myGeeks() {                     var GFG_Var = "GFG";            var h3 = document.getElementById("div");                         if (typeof GFG_Var === 'undefined') {                h3.innerHTML = "Variable is Undefined";            }            else {                h3.innerHTML = "Variable is defined and"                    + " value is " + GFG_Var;            }        }    </script></body> </html>                   
Output:  
Before clicking the button:  
After clicking the button:  
Example 3: The previous example doesn’t check for null value of variable. This example also checks a variable is null or not.  
html
<!DOCTYPE html><html> <head>    <title>        JavaScript to check existence of variable    </title></head>         <body style = "text-align:center;">         <h1 style = "color:green;" >        GeeksForGeeks    </h1>                  <p>        variable-name : GFG_Var    </p>      <button onclick="myGeeks()">        Check    </button>         <h3 id = "div" style="color:green;"></h3>                 <!-- Script to check existence of variable -->    <script>        function myGeeks() {                  var GFG_Var = null;            var h3 = document.getElementById("div");                         if (typeof GFG_Var === 'undefined' || GFG_Var === null ) {                h3.innerHTML = "Variable is Undefined";            }            else {                h3.innerHTML = "Variable is defined and value is "                        + GFG_Var;            }        }    </script></body> </html>                   
Output:  
Before clicking the button:  
After clicking the button:  
 
ruhelaa48
javascript-basics
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?