You want the typeof operator. Specifically:

if (typeof variable !== 'undefined') {
    // the variable is defined
}
Answer from Jim Puls on Stack Overflow
Top answer
1 of 8
117

A variable is declared if accessing the variable name will not produce a ReferenceError. The expression typeof variableName !== 'undefined' will be false in only one of two cases:

  • the variable is not declared (i.e., there is no var variableName in scope), or
  • the variable is declared and its value is undefined (i.e., the variable's value is not defined)

Otherwise, the comparison evaluates to true.

If you really want to test if a variable is declared or not, you'll need to catch any ReferenceError produced by attempts to reference it:

var barIsDeclared = true; 
try{ bar; }
catch(e) {
    if(e.name == "ReferenceError") {
        barIsDeclared = false;
    }
}

If you merely want to test if a declared variable's value is neither undefined nor null, you can simply test for it:

if (variableName !== undefined && variableName !== null) { ... }

Or equivalently, with a non-strict equality check against null:

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

Both your second example and your right-hand expression in the && operation tests if the value is "falsey", i.e., if it coerces to false in a boolean context. Such values include null, false, 0, and the empty string, not all of which you may want to discard.

2 of 8
20

It is important to note that 'undefined' is a perfectly valid value for a variable to hold. If you want to check if the variable exists at all,

if (window.variableName)

is a more complete check, since it is verifying that the variable has actually been defined. However, this is only useful if the variable is guaranteed to be an object! In addition, as others have pointed out, this could also return false if the value of variableName is false, 0, '', or null.

That said, that is usually not enough for our everyday purposes, since we often don't want to have an undefined value. As such, you should first check to see that the variable is defined, and then assert that it is not undefined using the typeof operator which, as Adam has pointed out, will not return undefined unless the variable truly is undefined.

if ( variableName  && typeof variableName !== 'undefined' )
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-check-if-a-variable-exists-or-defined-in-javascript.php
How to Check If a Variable Exists or Defined in JavaScript
<script> var x; var y = 10; if(typeof x !== 'undefined'){ // this statement will not execute alert("Variable x is defined."); } if(typeof y !== 'undefined'){ // this statement will execute alert("Variable y is defined."); } // Attempt to access an undeclared z variable if(typeof z !== 'undefined'){ // this statement will not execute alert("Variable z is defined."); } /* Throws Uncaught ReferenceError: z is not defined, and halt the execution of the script */ if(z !== 'undefined'){ // this statement will not execute alert("Variable z is defined."); } /* If the following statement runs, it will also throw the Uncaught ReferenceError: z is not defined */ if(z){ // this statement will not execute alert("Variable z is defined."); } </script>
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-check-the-existence-of-variable
JavaScript Check the existence of variable - GeeksforGeeks
July 11, 2025 - Example 2: This example also checks whether a variable is defined or not. ... let GFG_Var = "GFG"; if (typeof GFG_Var === 'undefined') { console.log("Variable is Undefined"); } else { console.log("Variable is defined and" + " value is " + GFG_Var); }
🌐
Sentry
sentry.io › sentry answers › javascript › javascript check if variable exists (is defined/initialized)
JavaScript check if variable exists (is defined/initialized) | Sentry
To check that a variable is defined, avoiding false positives and false negatives, we must check that its type is not undefined, using the typeof operator and strict inequality: ... let myVariable; if (typeof myVariable !== "undefined") { ...
🌐
TutorialsPoint
tutorialspoint.com › How-to-check-if-a-variable-exists-in-JavaScript
How to check if a variable exists in JavaScript?
To check if a variable exists in JavaScript, you need to check it against null as in the following code. Here, we’re checking the existence of variable myVar − <html>
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-defined-variable-checking
3 Ways to Check if a Variable is Defined in JavaScript
How to check if a variable is defined in JavaScript using typeof operator, try/catch blocks, or window.hasOwnProperty().
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › How-can-I-check-whether-a-variable-is-defined-in-JavaScript
How can I check whether a variable is defined in JavaScript?
For undefined variables, the typeof operator returns the string “undefined”. We can use the string equality (“===”) operator, and we can compare the returned value from the typeof operator with the “undefined” string and check the existence of the variable.
🌐
DEV Community
dev.to › jkvyff › checking-if-a-variable-exists-3pk6
Checking if a Variable Exists - DEV Community
December 14, 2019 - 'exists': 'nope'); // exists console.log(y !== undefined ? 'exists': 'nope'); // nope console.log(z !== undefined ? 'exists': 'nope'); // ReferenceError: z is not defined · Closer, but now we are casting types again, and still doing the comparison that breaks on undeclared variables
🌐
CodingDeft
codingdeft.com › posts › javascript-check-if-variable-defined
How to check if a variable is defined in JavaScript | CodingDeft.com
Tutorial on how check if a variable is defined, if a property exists in an object, and if a property exists in window.
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript check if a variable exists | defined/initialized example
JavaScript check if a variable exists | Undefined example code - EyeHunts
August 11, 2021 - <!DOCTYPE html> <html> <head> <script type="text/javascript"> var num; if (typeof num === 'undefined' || num === null) { alert("variable is undefined or null") } </script> </head> <body> </body> </html>
🌐
Mkyong
mkyong.com › home › javascript › check if variable is exists in javascript
Check if variable is exists in JavaScript - Mkyong.com
August 29, 2012 - <html> <body> <h1>JavaScript : typeof example</h1> <script type="text/javascript"> var str1 = "mkyong.com"; if(typeof str1 == 'string'){ document.write(str1 + " is a string <br/>"); } if(typeof str1 == 'undefined'){ document.write("str1 variable is not exists <br/>"); }else{ document.write("str1 variable is exists <br/>"); } if(typeof str2 == 'undefined'){ document.write("str2 variable is not exists <br/>"); }else{ document.write("str2 variable is exists <br/>"); } </script> </body> </html>
🌐
DEV Community
dev.to › collegewap › how-to-check-if-a-variable-is-defined-in-javascript-42p2
How to check if a variable is defined in JavaScript - DEV Community
November 24, 2022 - It can also be checked if the variable is initialized or not. In the below code, the control will not go inside the if block. let x if (typeof x !== "undefined") { console.log(x) } You will see the value of x logged into the console, if you execute the following code: let x = 1 if (typeof x !== "undefined") { console.log(x) } If you need to check if a property exists within an object then you can use the prototype method hasOwnProperty
🌐
SheCodes
shecodes.io › athena › 10489-check-if-an-object-exists-in-javascript
[JavaScript] - Check if an Object Exists in JavaScript - | SheCodes
Learn how to check if an object exists in JavaScript using the typeof operator to check if a variable is undefined.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-whether-an-object-exists-in-javascript
How to check whether an object exists in javascript ? - GeeksforGeeks
July 12, 2025 - Here we have some common approaches to check whether an object exists in javascript: ... The typeof operator in JavaScript returns the type of a variable as a string. If an object doesn't exist, typeof will return undefined.
🌐
Studytonight
studytonight.com › javascript-howtos › how-to-check-if-a-variable-exists-or-defined-in-javascript
How to check if a variable exists or defined in JavaScript - Studytonight
In the given example, we have used the typeof operator along with the strict equality operator to check whether the variable exists or not. <script> if (typeof myVar === 'undefined') { console.log('The variable myVar is undefined'); } else { ...