In JavaScript, null is an object. There's another value for things that don't exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, but in JavaScript itself undefined is the value used.

Second, no, there is not a direct equivalent. If you really want to check for specifically for null, do:

Copyif (yourvar === null) // Does not execute if yourvar is `undefined`

If you want to check if a variable exists, that can only be done with try/catch, since typeof will treat an undeclared variable and a variable declared with the value of undefined as equivalent.

But, to check if a variable is declared and is not undefined:

Copyif (yourvar !== undefined) // Any scope

Previously, it was necessary to use the typeof operator to check for undefined safely, because it was possible to reassign undefined just like a variable. The old way looked like this:

Copyif (typeof yourvar !== 'undefined') // Any scope

The issue of undefined being re-assignable was fixed in ECMAScript 5, which was released in 2009. You can now safely use === and !== to test for undefined without using typeof as undefined has been read-only for some time.

If you want to know if a member exists independent but don't care what its value is:

Copyif ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance

If you want to to know whether a variable is truthy:

Copyif (yourvar)

Source

Answer from Natrium on Stack Overflow
🌐
freeCodeCamp
freecodecamp.org › news › javascript-check-if-undefined-how-to-test-for-undefined-in-js
JavaScript Check if Undefined – How to Test for Undefined in JS
November 7, 2024 - You'll typically assign a value to a variable after you declare it, but this is not always the case. When a variable is declared or initialized but no value is assigned to it, JavaScript automatically displays "undefined".
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript - MDN Web Docs - Mozilla
It is one of JavaScript's primitive types. function test(t) { if (t === undefined) { return "Undefined value!"; } return t; } let x; console.log(test(x)); // Expected output: "Undefined value!" The primitive value undefined. undefined is a property of the global object.
Discussions

Good way to check for variable being not null and not undefined.
There are some style guides that basically say you always should use ===, but you can use == in order to check for null or undefined at the same time. So you could do the following: if (value != null) { // This will run if `value` is not `null` and not `undefined`. } More on reddit.com
🌐 r/javascript
56
32
October 20, 2016
How to check a not-defined variable in JavaScript - Stack Overflow
A better duplicate is how to check if a variable exist in javascript?. ... Your error is due to the variable not being declared. Most answers are focused on assignment. See my answer for more. Additionally many of them incorrectly state that null and undefined are objects in JavaScript. More on stackoverflow.com
🌐 stackoverflow.com
How can I check for "undefined" in JavaScript? - Stack Overflow
If it is undefined, it will not ... is not undefined. ... Sometimes you don't even have to check the type. If the value of the variable can't evaluate to false when it's set (for example if it's a function), then you can just evalue the variable. Example: ... No need for the parentheses: typeof is an operator, not a function. 2010-06-06T20:23:53.107Z+00:00 ... @Tim: @Nick is correct. See developer.mozilla.org/en/Core_Javascript_1.5_Reference/… ... More on stackoverflow.com
🌐 stackoverflow.com
How to check if a JavaScript variable is NOT undefined? - Stack Overflow
if "if(typeof lastName !== "undefined")" is not working for you, you may want to check your code for other problems More on stackoverflow.com
🌐 stackoverflow.com
🌐
BrowserStack
browserstack.com › home › guide › how to check if a variable is undefined in javascript
How to Check if a Variable is Undefined in JavaScript | BrowserStack
February 18, 2025 - In JavaScript, undefined is the default value for variables that have been declared but not initialized. On the other hand, null is an intentional assignment that explicitly indicates the absence of a value. ... This article covers different ways to check if a variable is undefined, helping you write code that handles these situations smoothly.
🌐
Stack Abuse
stackabuse.com › javascript-check-if-variable-is-a-undefined-or-null
JavaScript: Check if Variable is undefined or null
March 29, 2023 - If you're using a non-existent reference variable - silently ignoring that issue by using typeof might lead to silent failure down the line. The typeof operator can additionally be used alongside the === operator to check if the type of a variable is equal to 'undefined' or 'null':
🌐
Index.dev
index.dev › blog › check-undefined-variable-javascript
JavaScript Check if Undefined: 6 Methods to Check Variable Type
January 21, 2025 - Here are the fastest methods: javascript // Method 1: typeof (safest - works for undeclared variables) if (typeof myVar === 'undefined') { console.log('myVar is undefined'); } // Method 2: Strict equality (for declared variables) let myVar; ...
🌐
Medium
medium.com › deno-the-complete-reference › five-ways-to-check-for-undefined-in-javascript-b5568090df77
Five ways to check for undefined in JavaScript | Tech Tonic
March 10, 2024 - Use strict equality to compare the result with the string "undefined". This can be useful in cases where you need to handle other data types that might evaluate to false in an if statement (e.g., 0, empty string).
Find elsewhere
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-determine-if-variable-is-undefined-or-null-in-javascript.php
How to Determine If Variable is Undefined or NULL in JavaScript
Note: The undefined is not a reserved ... test undefined variable or property is using the typeof operator, like this: if(typeof myVar === 'undefined')....
Top answer
1 of 15
1875

In JavaScript, null is an object. There's another value for things that don't exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, but in JavaScript itself undefined is the value used.

Second, no, there is not a direct equivalent. If you really want to check for specifically for null, do:

Copyif (yourvar === null) // Does not execute if yourvar is `undefined`

If you want to check if a variable exists, that can only be done with try/catch, since typeof will treat an undeclared variable and a variable declared with the value of undefined as equivalent.

But, to check if a variable is declared and is not undefined:

Copyif (yourvar !== undefined) // Any scope

Previously, it was necessary to use the typeof operator to check for undefined safely, because it was possible to reassign undefined just like a variable. The old way looked like this:

Copyif (typeof yourvar !== 'undefined') // Any scope

The issue of undefined being re-assignable was fixed in ECMAScript 5, which was released in 2009. You can now safely use === and !== to test for undefined without using typeof as undefined has been read-only for some time.

If you want to know if a member exists independent but don't care what its value is:

Copyif ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance

If you want to to know whether a variable is truthy:

Copyif (yourvar)

Source

2 of 15
405

The only way to truly test if a variable is undefined is to do the following. Remember, undefined is an object in JavaScript.

Copyif (typeof someVar === 'undefined') {
  // Your variable is undefined
}

Some of the other solutions in this thread will lead you to believe a variable is undefined even though it has been defined (with a value of NULL or 0, for instance).

🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › undefined-check
How to Check if a JavaScript Variable is Undefined - Mastering JS
February 25, 2021 - If you want to check if x is strictly equal to undefined regardless of whether is has been declared or not, you should use typeof x === 'undefined'.
🌐
Sentry
sentry.io › sentry answers › javascript › how can i check for "undefined" in javascript?
How can I Check for "undefined" in JavaScript? | Sentry
You can use the strict equality operator (===) to check if a value is undefined: ... An interesting thing to note is that undefined is not a reserved word in JavaScript. A reserved word is a keyword that can’t be used as an identifier for ...
🌐
Programiz
programiz.com › javascript › examples › check-undefined-null
JavaScript Program To Check If A Variable Is undefined or null
To understand this example, you ... is undefined or null function checkVariable(variable) { if(variable == null) { console.log('The variable is undefined or null'); } else { console.log('The variable is neither undefined nor null'); } } let newVariable; checkVariable(5); ...
Top answer
1 of 16
3238

If you are interested in finding out whether a variable has been declared regardless of its value, then using the in operator is the safest way to go. Consider this example:

// global scope
var theFu; // theFu has been declared, but its value is undefined
typeof theFu; // "undefined"

But this may not be the intended result for some cases, since the variable or property was declared but just not initialized. Use the in operator for a more robust check.

"theFu" in window; // true
"theFoo" in window; // false

If you are interested in knowing whether the variable hasn't been declared or has the value undefined, then use the typeof operator, which is guaranteed to return a string:

if (typeof myVar !== 'undefined')

Direct comparisons against undefined are troublesome as undefined can be overwritten.

window.undefined = "foo";
"foo" == undefined // true

As @CMS pointed out, this has been patched in ECMAScript 5th ed., and undefined is non-writable.

if (window.myVar) will also include these falsy values, so it's not very robust:

false
0
""
NaN
null
undefined

Thanks to @CMS for pointing out that your third case - if (myVariable) can also throw an error in two cases. The first is when the variable hasn't been defined which throws a ReferenceError.

// abc was never declared.
if (abc) {
    // ReferenceError: abc is not defined
} 

The other case is when the variable has been defined, but has a getter function which throws an error when invoked. For example,

// or it's a property that can throw an error
Object.defineProperty(window, "myVariable", { 
    get: function() { throw new Error("W00t?"); }, 
    set: undefined 
});
if (myVariable) {
    // Error: W00t?
}
2 of 16
1588

I personally use

myVar === undefined

Warning: Please note that === is used over == and that myVar has been previously declared (not defined).


I do not like typeof myVar === "undefined". I think it is long winded and unnecessary. (I can get the same done in less code.)

Now some people will keel over in pain when they read this, screaming: "Wait! WAAITTT!!! undefined can be redefined!"

Cool. I know this. Then again, most variables in Javascript can be redefined. Should you never use any built-in identifier that can be redefined?

If you follow this rule, good for you: you aren't a hypocrite.

The thing is, in order to do lots of real work in JS, developers need to rely on redefinable identifiers to be what they are. I don't hear people telling me that I shouldn't use setTimeout because someone can

window.setTimeout = function () {
    alert("Got you now!");
};

Bottom line, the "it can be redefined" argument to not use a raw === undefined is bogus.

(If you are still scared of undefined being redefined, why are you blindly integrating untested library code into your code base? Or even simpler: a linting tool.)


Also, like the typeof approach, this technique can "detect" undeclared variables:

if (window.someVar === undefined) {
    doSomething();
}

But both these techniques leak in their abstraction. I urge you not to use this or even

if (typeof myVar !== "undefined") {
    doSomething();
}

Consider:

var iAmUndefined;

To catch whether or not that variable is declared or not, you may need to resort to the in operator. (In many cases, you can simply read the code O_o).

if ("myVar" in window) {
    doSomething();
}

But wait! There's more! What if some prototype chain magic is happening…? Now even the superior in operator does not suffice. (Okay, I'm done here about this part except to say that for 99% of the time, === undefined (and ****cough**** typeof) works just fine. If you really care, you can read about this subject on its own.)

🌐
Codereadability
codereadability.com › how-to-check-for-undefined-in-javascript
How to check for undefined in JavaScript
September 21, 2015 - When a variable is declared without being assigned a value its initial value is undefined. How do you check if a value is undefined in JavaScript? The short answer In modern browsers you can safely compare the variable directly to undefined: if (name === undefined) {...} Some people argue against ...
🌐
W3docs
w3docs.com › javascript
How to Check if the Variable is Undefined
If you check by value, you will get that variable is assigned a value or not. In the case of undefined, the assigned variable don’t have any value but the variable exists. Checking the type is done with the typeof operator.
🌐
Fireship
fireship.dev › check-for-undefined-javascript
How to check for undefined in JavaScript - Fireship
The way I recommend to check for undefined in JavaScript is using the strict equality operator, ===, and comparing it to the primitive undefined. ... Checking for `undefined`` this way will work in every use case except for one, if the variable ...
🌐
W3Schools
w3schools.com › jsref › jsref_undefined.asp
JavaScript undefined Property
❮ Previous JavaScript Global Methods Next ❯ · Variable with no value: let x; if (x === undefined) { text = "x is undefined"; } else { text = "x is defined"; } Try it Yourself » · let x; if (typeof x === "undefined") { text = "x is undefined"; } else { text = "x is defined"; } Try it Yourself » · More examples below. The undefined property indicates that a variable has not been assigned a value, or not declared at all.
🌐
SheCodes
shecodes.io › athena › 81408-what-does-undefined-mean-in-javascript
[JavaScript] - What does !==undefined mean in JavaScript? - | SheCodes
Learn about the !==undefined comparison operator in JavaScript and how it is used to check if a variable is not undefined.