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

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
A variable that has not been assigned a value is of type undefined. A function returns undefined if a value was not returned. Accessing a property that does not exist also returns undefined.
🌐
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, ...
🌐
Reddit
reddit.com › r/javascript › the difference between “undefined” and “not defined” in javascript
r/javascript on Reddit: The difference between “undefined” and “not defined” in JavaScript
October 3, 2022 - Undefined : a variable is declared , it has its own placeholder but not having the value of itself 'defined' hence undefined and until th variable has assigned a value , the undefined fills tht perticular placeholder. 'Not defined ' : this case comes in error where Js engine neither find that particular variable nor its placeholder and cannot find the variable in first phase of context (memory allocation context) ... Hi everybody, in this article I write about the difference of undeclared vs. unassigned variables in JavaScript.
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
Nullish coalescing is another JavaScript feature that also works well with TypeScript's null handling. It allows writing expressions that have a fallback specifically when dealing with null or undefined. This is useful when other falsy values can occur in the expression but are still valid. It can be used with the ?? operator in an expression, similar to using the && operator. function printMileage(mileage: number | null | undefined) { console.log(`Mileage: ${mileage ?? 'Not Available'}`); } printMileage(null); // Prints 'Mileage: Not Available' printMileage(0); // Prints 'Mileage: 0' Try it Yourself »
🌐
Medium
medium.com › @chandrashekharsingh25 › undefined-and-null-in-javascript-c09ff9913fdb
undefined and null in Javascript. In JavaScript, undefined and null are… | by Chandrashekhar Singh | Medium
May 12, 2023 - In conclusion, undefined and null are both primitive values in JavaScript that represent the absence of a meaningful value. While they may seem similar at first glance, they have different use cases and behaviors.
Find elsewhere
🌐
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.
🌐
Vercel
namaste-javascript-handbook.vercel.app › episode 6: undefined vs not defined in js
Episode 6: undefined vs not defined in JS | Namaste JavaScript Handbook
The value of a variable that hasn't ... is assigned a different value. undefined means that memory has been allocated to a variable but no value has been assigned yet. not defined refers to a variable that has not been declared ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › explain-the-difference-between-undefined-and-not-defined-in-javascript
Explain the difference between undefined and not defined in JavaScript - GeeksforGeeks
July 23, 2025 - Example 2: First of all, global execution context will be created and in the memory allocation phase, the variable "a" will get space in memory, and by default, JavaScript assigns undefined to "a". During the thread of execution, the "console.log(a)" will be printed as undefined. In the next line, we have assigned 5 to variable a. In the console, 5 will be printed. At the last line when JavaScript encounters the "console.log(b)" it searches for "b" inside the memory heap of execution context but it is not available, the JS engine will throw the "Reference Error" with a message of "b is not defined".
🌐
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:
🌐
JavaScript in Plain English
javascript.plainenglish.io › the-real-reason-you-keep-getting-undefined-errors-in-javascript-9e226923c513
The Real Reason You Keep Getting undefined Errors in JavaScript | by Ferid Brković | JavaScript in Plain English
December 4, 2024 - const fruits = { banana: { color: "yellow" }, orange: { color: "orange" } }; console.log(fruits.apple.color); // undefined ... New JavaScript and Web Development content every day.
Top answer
1 of 5
16

There are two things you should understand about undefined:

  • the type undefined that can have only one value
  • the variable undefined

To explain:

  • There are so many values of type number (10, 10.01, 1e1). But there can be only one value of type undefined, and that value is stored in the variable undefined.

  • That value has no literal representation either. For example, number values 1, 100, 1e-1 are all literals of type number, but the value stored in the variable undefined has no literal form.

  • undefined is a variable, just a normal variable, that JavaScript declares and assigns it the value of type undefined in the global scope. So you can do all the following...

    typeof undefined;                       // "undefined"
    
    undefined = 100;
    typeof undefined;                       // "number"
    
    undefined = void 0;
    typeof undefined;                       // "undefined"
    
    window.undefined === undefined;         // true
    window.undefined === void 0;            // true
    
  • if you don't want to use the variable undefined, you can generate the value of type undefined by the expression void 0 -- whose sole purpose is to return a value of type undefined.

...can anyone please explain to me why this thing has been inserted into JavaScript...

JavaScript has had a history of bad design - not because of the people who designed it but because no one could foresee that this little scripting capability they were adding to Netscape would one day underpin the business of billion dollar companies.

...we have null value...

Although null can do things undefined does, it is more or less related to objects rather than scalars. Indeed, JavaScript considers null itself an object -- typeof null returns "object".

2 of 5
10

Sorry for answering an older question but the reason you need both undefined and null is simple: in a prototype-based duck-typing language you absolutely must differentiate between "this object does not define a value for X" and "this object says X is nothing/null/empty".

Without this capability there is no way to walk the prototype chain and so inheritance can't work; you must be able to determine that obj.someProp is undefined so you can look at obj.prototype.someProp, and onward up the chain until you find a value. If obj.someProp returned null there would be no way to know if it really was null or just meant "look at my prototype". The only other way around this is to inject some kludgy magic behind the scenes which breaks your ability to fuzz with the prototype chains and do various other bits of JS black magic.

Like much of Javascript, the idea of undefined and null seems wonky and stupid at first, then absolutely brilliant later (then back to wonky and stupid but with a reasonable explanation).

A language like C# will not compile if you access a property that doesn't exist and other dynamic languages often throw exceptions at runtime when you touch a property that doesn't exist, meaning you have to use special constructs to test for them. Plus classes means when an object is instantiated you already know its inheritance chain and all the properties it has - in JS I can modify a prototype 15 steps up the chain and those changes will appear on existing objects.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Errors › Not_defined
ReferenceError: "x" is not defined - JavaScript | MDN
function numbers() { const num1 = 2; const num2 = 3; return num1 + num2; } console.log(num1); // ReferenceError num1 is not defined.
🌐
Medium
medium.com › front-end-weekly › beginners-guide-dealing-with-undefined-in-javascript-d98ac7e413db
Beginner’s Guide: Dealing with Undefined in JavaScript | by Brandon Evans | Frontend Weekly | Medium
June 8, 2023 - The in operator in JavaScript allows you to check if an object has a specific property. It is particularly useful when you want to verify the existence of a property and avoid accessing undefined properties.
🌐
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.
🌐
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: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · 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
🌐
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/
🌐
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.