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

Answer from treecoder on Stack Exchange
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript - MDN Web Docs - Mozilla
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. The void operator always returns undefined. Note: While you can use undefined as an identifier (variable ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › undefined-in-javascript
Undefined in JavaScript - GeeksforGeeks
March 13, 2024 - Undefined is a type of Data type in JavaScript. It is a primitive value undefined, when a variable is declared and not initialized or not assigned with any value.
Discussions

language design - What exactly undefined means in JavaScript? Why it's there? What usages it has? How it could be useful? - Software Engineering Stack Exchange
Can anyone please explain to me ... into JavaScript? We have null value in this language, thus it shouldn't be something like null. In other languages, when we don't know the value of a property or variable, we simply set it to null. Here we can do the same thing. ... 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 ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
The difference between “undefined” and “not defined” in JavaScript
Good read! You could mention that before strict mode, "not defined" was not a thing. You could reference a variable that was never defined, and it would simply create a new variable and add it to the global scope. That was a massive footgun that made typos in variable names hard to detect, so "Not defined" was added to help developers. More on reddit.com
🌐 r/javascript
19
86
October 3, 2022
What is the difference between null and undefined in JavaScript? - Stack Overflow
I recommend only setting variables to null and leave undefined the value for things you forgot to set. At the same time, I really encourage you to always set every variable. JavaScript has a scope chain different than that of C-style languages, easily confusing even veteran programmers, and setting variables to null is the best way to prevent bugs based on it. Another instance ... More on stackoverflow.com
🌐 stackoverflow.com
Undefined vs null
One reason to favor undefined over null is how javascript handle default values: const withDefault = (a = true) => { console.log(a); }; withDefault(); // logs true withDefault(undefined); // logs true withDefault(null); // logs null More on reddit.com
🌐 r/typescript
51
46
February 27, 2023
🌐
W3Schools
w3schools.com › jsref › jsref_undefined.asp
JavaScript undefined Property
The undefined property indicates that a variable has not been assigned a value, or not declared at all. undefined() is an ECMAScript1 (JavaScript 1997) feature.
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 › Glossary › Undefined
Undefined - Glossary - MDN Web Docs
undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.
🌐
Mimo
mimo.org › glossary › javascript › undefined
JavaScript undefined: Syntax, Usage, and Examples
When you declare a variable without assigning a value, JavaScript sets it to undefined: ... The variable count exists, but no value was stored in it. That is the default undefined JavaScript state.
🌐
Educative
educative.io › answers › what-is-undefined-vs-not-defined-in-javascript
What is undefined vs not defined in JavaScript?
While undefined indicates an existing variable without a value, not defined takes it a step further to represent variables that don’t exist at all. Let’s explore this concept.
Find elsewhere
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript undefined
JavaScript undefined
October 6, 2023 - JavaScript also has the null value. In addition, it has the undefined value. And both null and undefined values represent empty values in JavaScript. The undefined is a primitive type in JavaScript. So the undefined is a type.
🌐
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 - When a variable is declared or initialized but no value is assigned to it, JavaScript automatically displays "undefined".
🌐
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.
🌐
Musing Mortoray
mortoray.com › home › the many faces of undefined in javascript
The many faces of undefined in JavaScript - Musing Mortoray
June 26, 2024 - But not JavaScript; we’re just getting started. The concept of undefined initially seems to make sense. Whereas null specifies an actual value, an undefined would indicate the value doesn’t actually exist. This makes sense if you consider this structure, using TypeScript notation: interface Arguments { nullable_value: number | null optional_value?: number | null } We’re saying that nullable_value is a required property of this structure: you can’t omit it.
🌐
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.
🌐
Medium
medium.com › @pruthvimandaliya007 › all-you-need-to-know-about-keyword-undefined-in-javascript-02562952fc22
All you need to know about keyword ‘Undefined’ in JavaScript | by Pruthvi Mandaliya | Medium
January 10, 2024 - The assignment of an ‘Undefined’ to a variable does not mean that variable is empty or it is not allocated any memory space in the heap memory. I would term it as, the JavaScript makes use of this special keyword ‘Undefined’ while there is no other value available to assign it to a JavaScript variable.
🌐
Wordpress
javascriptweblog.wordpress.com › 2010 › 08 › 16 › understanding-undefined-and-preventing-referenceerrors
Understanding JavaScript's 'undefined'
September 4, 2010 - The only similarity between undefined and null is they both coerce to false. A ReferenceError indicates that an invalid reference value has been detected (ECMA 5 15.11.6.3) In practical terms, this means a ReferenceError will be thrown when JavaScript attempts to get the value of an unresolvable reference.
🌐
Medium
joinvnexus.medium.com › what-is-undefined-in-javascript-055667635811
What is undefined in JavaScript? - VueNexus - Medium
September 13, 2025 - It’s one of those values that shows up everywhere, sometimes when you least expect it. So, what exactly does it mean? Let’s break it down. undefined is a built-in primitive value in JavaScript.
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
Unlike the reserved keyword null, undefined is a property of the global object. This was a design decision made early in JavaScript's development, and it let legacy browsers overwrite undefined completely. In modern browsers, it's still possible to use undefined as an identifier in non-global scopes, overriding its value within the scope of that declaration.
🌐
Scaler
scaler.com › home › topics › javascript › null and undefined in javascript
Null and Undefined in JavaScript - Scaler Topics
April 4, 2024 - Null in JavaScript means an empty ... null contains no value. Undefined, on the other hand, means the variable has been declared, but its value has not been assigned....
🌐
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.
🌐
Syncfusion
syncfusion.com › blogs › javascript › null vs. undefined in javascript
Null vs. Undefined in JavaScript | Syncfusion Blogs
December 10, 2024 - Syncfusion JavaScript controls allow you to build powerful line-of-business applications. ... The following instances depict when and how you could utilize null and undefined in a meaningful manner based on the properties discussed so far: Since undefined is the default value assigned by JavaScript to uninitialized variables, if you want to indicate the absence of a deal explicitly, always use null instead of undefined to avoid confusion.