typeof is safer as it allows the identifier to never have been declared before:

if(typeof neverDeclared === "undefined") // no errors

if(neverDeclared === null) // throws ReferenceError: neverDeclared is not defined
Answer from seanmonstar on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
In all non-legacy browsers, undefined is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it. A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does ...
🌐
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.
🌐
W3Schools
w3schools.com › Jsref › jsref_undefined.asp
JavaScript undefined Property
altKey (Mouse) altKey (Key) animationName bubbles button buttons cancelable charCode clientX clientY code ctrlKey (Mouse) ctrlKey (Key) currentTarget data defaultPrevented deltaX deltaY deltaZ deltaMode detail elapsedTime elapsedTime eventPhase inputType isTrusted key keyCode location metaKey (Mouse) metaKey (Key) newURL oldURL offsetX offsetY pageX pageY persisted propertyName relatedTarget relatedTarget screenX screenY shiftKey (Mouse) shiftKey (Key) target targetTouches timeStamp touches type which (Mouse) which (Key) view HTML Event Methods
🌐
W3Schools
w3schools.com › js › js_typeof.asp
JavaScript typeof
The typeof of a variable with no value is undefined. The value is also undefined. ... Any variable can be emptied, by setting the value to undefined. The type will also be undefined. let car = "Volvo"; car = undefined; Try it Yourself » · ...
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
The strict equality operator considers operands of different data types to be unequal. null == undefined > true null === undefined > false · Unlike the reserved keyword null, undefined is a property of the global object.
🌐
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.
🌐
CoreUI
coreui.io › blog › what-is-the-difference-between-null-and-undefined-in-javascript
What is the Difference Between Null and Undefined in JavaScript · CoreUI
February 9, 2025 - Unlike null, undefined means JavaScript cannot find a meaningful value. In contrast, null means a developer has explicitly assigned an empty value. The value null signals the intentional absence of data or object value.
Find elsewhere
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Data types
July 9, 2024 - It makes a type of its own, just like null. The meaning of undefined is “value is not assigned”. If a variable is declared, but not assigned, then its value is undefined: ... …But we don’t recommend doing that.
🌐
Musing Mortoray
mortoray.com › home › the many faces of undefined in javascript
The many faces of undefined in JavaScript - Musing Mortoray
June 26, 2024 - This convention caused problems when I wrote a cross-language serialization format. To ease the Python side of things, I ended up treated any types as null | undefined in JavaScript that could have just been undefined.
🌐
Dmitri Pavlutin
dmitripavlutin.com › 7-tips-to-handle-undefined-in-javascript
7 Tips to Handle undefined in JavaScript
March 23, 2023 - Because JavaScript is permissive, developers have the temptation to access uninitialized values. I'm guilty of such bad practice too. Often such risky actions generate undefined related errors: ... To reduce such errors, you have to understand the cases when undefined is generated. Let's explore undefined and its effect on code safety. ... Undefined: undefined. And a separated object type: {name: "Dmitri"}, ["apple", "orange"].
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Data_structures
JavaScript data types and data structures - JavaScript | MDN
The object wrapper classes' reference pages contain more information about the methods and properties available for each type, as well as detailed descriptions for the semantics of the primitive types themselves. The Null type is inhabited by exactly one value: null. The Undefined type is inhabited ...
🌐
ui.dev
ui.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 ...
🌐
DEV Community
dev.to › sduduzog › null-vs-undefined-what-to-choose-what-to-use-11g
null vs undefined? What to choose? What to use? - DEV Community
August 23, 2023 - When a javascript object is being serialized, all undefined properties are discarded, remember 'undefined' means a property is yet to be assigned a value. But null on the other hand is known by JSON as its a valid JSON data type · That's it.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › variable-undefined-vs-typeof-variable-undefined-in-javascript
variable === undefined vs. typeof ...
July 12, 2025 - The type of operator does not throw an error if the variable has not been declared. Example: Here we assign two variables one is undefined and the other one is defined as "null", here null is not undefined when you put console.log it will show "null" if you check typeof then it will display the object, below program will illustrate the approach more clearly.
🌐
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. Asked over 2 years ago in JavaScript by Keamogetse ... Write a function that takes in a single number. It should return the string even if the number is even, and the string odd if the number is odd. ... on a page with just a button, how can I change the color of the whole background?
🌐
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 - 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 naming things such as variables, properties, or functions. Reserved words include: import, const, and return. 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:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › typeof
typeof - JavaScript | MDN
console.log(typeof 42); // Expected output: "number" console.log(typeof "blubber"); // Expected output: "string" console.log(typeof true); // Expected output: "boolean" console.log(typeof undeclaredVariable); // Expected output: "undefined" ... An expression representing the object or primitive whose type is to be returned. The following table summarizes the possible return values of typeof. For more information about types and primitives, see also the JavaScript data structure page.
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.