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

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-for-undefined-value-in-javascript
How to check for "undefined" value in JavaScript ? - GeeksforGeeks
July 23, 2025 - Here, the '===' operator checks if the value of the variable is exactly equal to 'undefined'. ... // Declare a variable let myVariable; // Condition to check variable is defined or not if (myVariable === undefined) { console.log("myVariable ...
🌐
ServiceNow Community
servicenow.com › community › in-other-news › undefined-null-and-oh-my › ba-p › 2291977
undefined, null, ==, !=, ===, and !== - Oh My! - ServiceNow Community
June 3, 2024 - The undefined value is actually implemented in JavaScript by a special Undefined object, which only has one instance (it's a singleton). Horace has an SSN property, because he's a U.S. citizen, but he forgot what it was — so we assigned a null for him (Horace is a null kinda guy, actually). Note that difference between null and undefined: the former indicates that the property is there, but it doesn't refer to any value at the moment; the latter says the property isn't even there.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
July 8, 2025 - The undefined global property represents the primitive value undefined. It is one of JavaScript's primitive types.
🌐
Educative
educative.io › answers › what-is-undefined-vs-not-defined-in-javascript
What is undefined vs not defined in JavaScript?
However, null === undefined is false since they are of different types (null is an object, and undefined is a primitive). A variable is considered “not defined” in JavaScript if it has not been declared in the code.
🌐
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 - Though both undefined and null represent “no value,” they are distinct types in JavaScript. undefined: This is the default value of uninitialized variables. It is automatically assigned when a variable is declared without a value.
Find elsewhere
🌐
Dmitri Pavlutin
dmitripavlutin.com › 7-tips-to-handle-undefined-in-javascript
7 Tips to Handle undefined in JavaScript
March 23, 2023 - The variables are not exposed to an uninitialized state, thus you have no risk of accessing undefined · Moving the variables as close as possible to their usage place increases the code readability · High cohesive chunks of code are easier to refactor and extract into separate functions, if necessary · When accessing a non-existing object property, JavaScript returns undefined.
🌐
W3Schools
w3schools.com › jsref › jsref_undefined.asp
JavaScript undefined Property
if (typeof y === "undefined") { txt = "y is undefined"; } else { txt = "y is defined"; } Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected] · If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected] · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Codedamn
codedamn.com › news › javascript
How to check if value is undefined or null in JavaScript
June 8, 2023 - The easiest way to check if a value is either undefined or null is by using the equality operator (==). The equality operator performs type coercion, which means it converts the operands to the same type before making the comparison.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.
🌐
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.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Undefined vs null - JavaScript - The freeCodeCamp Forum
November 19, 2019 - Hello. What is the difference between undefined and null?
🌐
Stack Abuse
stackabuse.com › javascript-check-if-variable-is-a-undefined-or-null
JavaScript: Check if Variable is undefined or null
March 29, 2023 - In this short guide, we've taken a look at how to check if a variable is null, undefined or nil in JavaScript, using the ==, === and typeof operators, noting the pros and cons of each approach.
🌐
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 - The typeof operator returns the data type of variable. Use strict equality to compare the result with the string "undefined".
🌐
Sentry
sentry.io › sentry answers › javascript › how can i check for "undefined" in javascript?
How can I Check for "undefined" in JavaScript? | Sentry
December 15, 2022 - You can use undefined as a variable name, as long as the variable is not in the global scope. As can be seen in the example code below, you can make the typeof undefined equal to string:
🌐
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 - An undefined variable or anything without a value will always return "undefined" in JavaScript. This is not the same as null, despite the fact that both imply an empty state. You'll typically assign a value to a variable after you declare it, ...
🌐
Quora
quora.com › Why-does-JavaScript-return-true-when-a-variable-is-not-defined
Why does JavaScript return true when a variable is not defined? - Quora
Answer (1 of 2): It doesn’t, or at least shouldn’t, since undefined is one of the falsy values. https://www.30secondsofcode.org/js/s/truthy-falsy-values/
🌐
Altcademy
altcademy.com › blog › how-to-check-undefined-in-javascript
How to check undefined in JavaScript
August 29, 2023 - This is the safest way as it does not throw an error even if the variable has not been declared. Use strict equality operator (===) to check for null or undefined. Loose equality operator (==) can lead to unexpected results due to type coercion.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
August 26, 2025 - In this example, we will provide default values but keep values other than null or undefined. ... const nullValue = null; const emptyText = ""; // falsy const someNumber = 42; const valA = nullValue ?? "default for A"; const valB = emptyText ?? "default for B"; const valC = someNumber ?? 0; console.log(valA); // "default for A" console.log(valB); // "" (as the empty string is not null or undefined) console.log(valC); // 42