JavaScript | Check the existence of variable
JavaScript has a bulit-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.
<!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.
<!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.
<!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:

Recommended Posts:
- JavaScript | Check if a variable is a string
- How to check if a variable is an array in JavaScript?
- How to check a variable is of function type using JavaScript ?
- How to check the existence of URL in PHP?
- variable === undefined vs. typeof variable === “undefined” in JavaScript
- JavaScript | Set object key by variable
- Understanding variable scopes in JavaScript
- How to use a variable for a key in a JavaScript object literal?
- How to change the value of a global variable inside of a function using JavaScript ?
- PHP | Check if a variable is a function
- Substring check in JavaScript
- How to check two elements are same using jQuery/JavaScript ?
- How to check a function is defined in JavaScript ?
- Check if an array is empty or not in JavaScript
- How to Check/Uncheck the checkbox using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



