How can I check whether a variable is defined in JavaScript?

In JavaScript, checking whether a variable is defined helps prevent ReferenceError exceptions. A variable can be declared but undefined, declared and defined, or completely undeclared.

Understanding the difference between these states is crucial:

const data = 10;    // declared and defined (initialized with value 10)
let data;           // declared but undefined
const data = null;  // declared and defined (initialized with null)
data = 10;          // undeclared but assigned (creates global variable)
data                // undeclared - throws ReferenceError

Here are the most reliable methods to check variable definition:

Using the typeof Operator (Recommended)

The typeof operator safely checks variables without throwing errors, even for undeclared variables.

Syntax

if (typeof variable_name === "undefined") {
    // Variable is undefined or undeclared
}

Example

<html>
<head>
   <title>Check if variable is defined</title>
</head>
<body>
   <h2>Using <i>typeof</i> operator to check variable definition</h2>
   <div id="result1"></div>
   <div id="result2"></div>
   
   <script>
      let age = 20;
      let name; // declared but undefined
      
      let result1 = document.getElementById("result1");
      let result2 = document.getElementById("result2");
      
      if (typeof age === "undefined") {
         result1.innerHTML = "Variable <i>age</i> is undefined";
      } else {
         result1.innerHTML = "Variable <i>age</i> is defined with value: " + age;
      }
      
      if (typeof name === "undefined") {
         result2.innerHTML = "Variable <i>name</i> is undefined";
      } else {
         result2.innerHTML = "Variable <i>name</i> is defined";
      }
   </script>
</body>
</html>

Using Strict Equality with undefined

This method works for declared variables but throws ReferenceError for undeclared ones.

<html>
<body>
   <h2>Using <i>if</i> statement with strict equality</h2>
   <div id="output"></div>
   
   <script>
      var rank; // declared but undefined
      let output = document.getElementById("output");
      
      if (rank === undefined) {
         output.innerHTML = "Variable <i>rank</i> is undefined";
      } else {
         output.innerHTML = "Variable <i>rank</i> is defined";
      }
   </script>
</body>
</html>

Using window.hasOwnProperty() Method

This method checks if a variable exists as a property of the global window object. It only works for var declarations and global variables, not let or const.

Syntax

let exists = window.hasOwnProperty('variable_name');

Example

<html>
<body>
   <h2>Using <i>window.hasOwnProperty()</i> method</h2>
   <div id="result"></div>
   
   <script>
      var globalVar = "exists";
      let result = document.getElementById("result");
      
      function checkVariable(varName) {
         if (window.hasOwnProperty(varName)) {
            return varName + " exists in global scope";
         } else {
            return varName + " doesn't exist in global scope";
         }
      }
      
      result.innerHTML = checkVariable('globalVar') + "<br>" + 
                        checkVariable('nonExistentVar');
   </script>
</body>
</html>

Using try/catch Block

The try/catch approach catches ReferenceError exceptions when accessing undeclared variables.

<html>
<body>
   <h2>Using <i>try/catch</i> block to check variable existence</h2>
   <div id="error-result"></div>
   
   <script>
      let errorResult = document.getElementById("error-result");
      
      function checkVariableExists() {
         try {
            let data = undeclaredVariable; // This will throw ReferenceError
            errorResult.innerHTML = "Variable <i>undeclaredVariable</i> exists";
         } catch (error) {
            errorResult.innerHTML = "Caught error: " + error.message;
         }
      }
      
      checkVariableExists();
   </script>
</body>
</html>

Comparison of Methods

Method Works with undeclared variables? Works with let/const? Best for
typeof Yes Yes Universal checking
=== undefined No (throws error) Yes Declared variables only
window.hasOwnProperty() Yes No Global var declarations
try/catch Yes Yes Error handling approach

Conclusion

Use typeof variable === "undefined" as the most reliable method for checking variable definition. It works safely with all variable types and declaration methods without throwing errors.

Updated on: 2026-03-15T21:36:28+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements