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:

if (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:

if (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:

if (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:

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

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

if (yourvar)

Source

Answer from Natrium on Stack Overflow
🌐
Reddit
reddit.com › r/javascript › good way to check for variable being not null and not undefined.
r/javascript on Reddit: Good way to check for variable being not null and not undefined.
October 20, 2016 -

Hi, all, I often do stuff like this in my code, to check for a variable being not null and not undefined.

// check if value is not null and not undefined
if (value) {
  ...
}

However, I'm now thinking this can leads to bugs, because of 0, "", false and NaN also being falsy.

What is a better way to check a variable is not null and not undefined? I could use this I think, wondering if there is something shorter than this:

if (typeof value !== 'undefined' || value !== null) {
  ...
}
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:

if (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:

if (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:

if (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:

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

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

if (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.

if (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).

Discussions

What's the best method to detect undefined variables in JS?
Hey everyone! I’m trying to figure out the most reliable way to check if a variable is undefined in JavaScript. I’ve seen a bunch of different approaches and I’m not sure which one is the best to use. Some people do thi… More on community.latenode.com
🌐 community.latenode.com
0
0
May 13, 2025
How to detect that a pointer is undefined?
With the release version of 0.12.0, pointers set to undefined no longer seem to have a known address. The following prints 0 in 0.11.0 and 0.12.0-dev.3635+786876c05: const std = @import("std"); const A = struct { p… More on ziggit.dev
🌐 ziggit.dev
0
0
April 20, 2024
jQuery / Javascript code check, if not undefined - Stack Overflow
var wlocation = $(this).closes... !== "undefined") { window.location = wlocation; } ... But if you prefer the second way wouldn't be as you posted. It would be: ... Sign up to request clarification or add additional context in comments. ... wlocation is just a string (the value of the href attribute) so it has not any prop ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Check if variable is false and not either true or undefined - Stack Overflow
What's the best way to check if myvar javascript variable === false or not (it may be undefined as well). if (myvar === false) would be fine but myvar could be undefined. Only false value is accep... More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
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.
🌐
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".
🌐
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.
🌐
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 ...
🌐
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 - Join Medium for free to get updates from this writer. ... This is the most common and recommended way. Use the strict equality operator ( === ) to compare the variable with the primitive value undefined.
Find elsewhere
🌐
Index.dev
index.dev › blog › check-undefined-variable-javascript
JavaScript Check if Undefined: 6 Methods to Check Variable Type
January 21, 2025 - With JavaScript being a loosely-typed language, understanding the nuances of checking variable definitions is critical for developers working on modern web applications, frameworks, and libraries. ... javascript // Declared but not assigned let x; console.log(x); // undefined // Explicit assignment let y = undefined; console.log(y); // undefined // Non-existent property const obj = { a: 1 }; console.log(obj.b); // undefined // Missing function parameter function greet(name) { console.log(name); // undefined if not passed } greet(); // No return value function noReturn() {} console.log(noReturn()); // undefined
🌐
DEV Community
dev.to › anthonybanion › checking-null-undefined-empty-and-falsy-values-in-javascript-2io5
Checking `null`, `undefined`, Empty, and Falsy Values in JavaScript - DEV Community
August 17, 2025 - In JavaScript, values are evaluated in Boolean contexts as either truthy or falsy. ... Everything else is considered truthy (including "0", "false", [], {}, etc.). ... if (!0) console.log("0 is falsy"); // ✅ if (!"") console.log("Empty string"); // ✅ if (!null) console.log("Null is falsy"); // ✅ · let user; if (user == null) { console.log("User is either null or undefined"); }
🌐
Fireship
fireship.dev › check-for-undefined-javascript
How to check for undefined in JavaScript
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 ...
🌐
LogRocket
blog.logrocket.com › home › how to check for null, undefined, or empty values in javascript
How to check for null, undefined, or empty values in JavaScript - LogRocket Blog
February 14, 2025 - Even if you were to do things in other languages that would throw, such as access an entry in an array that is out of bounds, C# would throw, whereas JavaScript would simply return undefined. ... We’ve got an empty array, and then we try to print out the fifth element from the array. This is out of bounds. The result of this code is undefined. Basically, there are three conditions that we want to account for when checking for values that we think could be null or undefined.
🌐
Latenode
community.latenode.com › other questions › javascript
What's the best method to detect undefined variables in JS? - JavaScript - Latenode Official Community
May 13, 2025 - Hey everyone! I’m trying to figure out the most reliable way to check if a variable is undefined in JavaScript. I’ve seen a bunch of different approaches and I’m not sure which one is the best to use. Some people do this: if (window.myVar) { // Do something } Others use: if (typeof someVar !== 'undefined') { // Do something } And sometimes I see: if (randomVar) { // But this might throw an error, right?
🌐
Ziggit
ziggit.dev › help
How to detect that a pointer is undefined? - Help - Ziggit
With the release version of 0.12.0, pointers set to undefined no longer seem to have a known address. The following prints 0 in 0.11.0 and 0.12.0-dev.3635+786876c05: const std = @import("std"); const A = struct { ptr: *u8 = undefined, }; pub fn main() void { inline for (@typeInfo(A).Struct.fields) |field| { if (field.default_value) |opaque_ptr| { const ptr_ptr: *const field.type = @ptrCast(@alignCast(opaque_ptr)); const ptr = ptr_ptr.*; std....
Published: April 20, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-check-if-a-javascript-variable-is-not-undefined
How to Check if a JavaScript Variable is NOT Undefined?
November 12, 2024 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Attacomsian
attacomsian.com › blog › javascript-check-if-variable-is-undefined-or-null
How to check if a variable is undefined or NULL in JavaScript
September 17, 2022 - To check if a variable is undefined or null in JavaScript, either use the equality operator == or strict equality operator ===. In JavaScript, a variable is considered undefined if it is declared but not assigned a value.
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
However, type guards are extended to support non-null and non-undefined checks. ... Non-null and non-undefined type guards may use the ==, !=, ===, or !== operator to compare to null or undefined, as in x != null or x === undefined. The effects on subject variable types accurately reflect ...
🌐
Sentry
sentry.io › sentry answers › javascript › javascript check if variable exists (is defined/initialized)
JavaScript check if variable exists (is defined/initialized) | Sentry
August 15, 2023 - 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") { ...
🌐
Lucybain
lucybain.com › blog › 2014 › null-undefined-undeclared
Lucy | JS: null, undefined, and undeclared
October 6, 2014 - Note that the typeof returns "undefined", therefore undefined is a primitive type. The fix for an undefined variable or function is easy, simply define it: You can know if a variable is undefined with the following: