From the MDN page about the behaviour of the typeof operator:

null

// This stands since the beginning of JavaScript
typeof null === 'object';

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the "object" typeof return value. (reference)

A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === 'null'.

Answer from Deepak Ingole on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
JavaScript is unique to have two nullish values: null and undefined. Semantically, their difference is very minor: undefined represents the absence of a value, while null represents the absence of an object. For example, the end of the prototype chain is null because the prototype chain is ...
🌐
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 » · An empty value has nothing to do with undefined. An empty string has both a legal value and a type. ... In JavaScript null is "nothing".
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › null-in-javascript
Null in JavaScript - GeeksforGeeks
June 5, 2024 - In JavaScript, `null` indicates the deliberate absence of any object value. It's a primitive value that denotes the absence of a value or serves as a placeholder for an object that isn't present.
🌐
Programiz
programiz.com › javascript › null-undefined
JavaScript null and undefined
In JavaScript, null is treated as an object. You can check this using the typeof operator. The typeof operator determines the type of variables and values.
🌐
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. This was a design decision made early in JavaScript's development, and it let legacy browsers overwrite undefined completely.
Top answer
1 of 16
1532
(name is undefined)

You: What is name? (*)
JavaScript: name? What's a name? I don't know what you're talking about. You haven't ever mentioned any name before. Are you seeing some other scripting language on the (client-)side?

name = null;

You: What is name?
JavaScript: I don't know.

In short; undefined is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope; null is where the thing is known to exist, but it's not known what the value is.

One thing to remember is that null is not, conceptually, the same as false or "" or such, even if they equate after type casting, i.e.

name = false;

You: What is name?
JavaScript: Boolean false.

name = '';

You: What is name?
JavaScript: Empty string


*: name in this context is meant as a variable which has never been defined. It could be any undefined variable, however, name is a property of just about any HTML form element. It goes way, way back and was instituted well before id. It is useful because ids must be unique but names do not have to be.

2 of 16
147

The difference can be summarized into this snippet:

alert(typeof(null));      // object
alert(typeof(undefined)); // undefined

alert(null !== undefined) //true
alert(null == undefined)  //true

Checking

object == null is different to check if ( !object ).

The latter is equal to ! Boolean(object), because the unary ! operator automatically cast the right operand into a Boolean.

Since Boolean(null) equals false then !false === true.

So if your object is not null, but false or 0 or "", the check will pass because:

alert(Boolean(null)) //false
alert(Boolean(0))    //false
alert(Boolean(""))   //false
🌐
2ality
2ality.com › 2013 › 10 › typeof-null.html
The history of “typeof null”
In JavaScript, typeof null is 'object', which incorrectly suggests that null is an object (it isn’t, it’s a primitive value, consult my blog post on categorizing values for details). This is a bug and one that unfortunately can’t be fixed, because it would break existing code.
Find elsewhere
🌐
DEV Community
dev.to › _ravo_lution › why-typeof-null-is-object-181
Why typeof(null) is "object"? - DEV Community
July 26, 2021 - ... null (JSVAL_NULL) was the machine code NULL pointer. Or: an object type tag plus a reference that is zero. **Let's look at the classic JavaScript Source code used in the '96 in SpiderMonkey courtesy: @evilpies: https://twitter.com/evilpies/
🌐
DEV Community
dev.to › elmarshall › javascript-typeof-null-52k0
JavaScript: typeof null - DEV Community
May 29, 2020 - As a result of this similarity, null has the 0 type tag, which corresponds to an object. The upshot of all this is, if you're writing a function and you need to check whether or not a value is null, you’ll need to use the === operator, not ...
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-null
Everything about null in JavaScript
null in JavaScript is a special value that represents a missing object.
🌐
bitsofcode
bitsofco.de › javascript-typeof
"What's the typeof null?", and other confusing JavaScript Types | bitsofcode
As a result of this similarity, null has the 0 type tag, which corresponds to an object. ... Finally, we have Classes. Classes were introduced in ES2015 (ES6) as a better syntax for prototype-based inheritance.
🌐
Scaler
scaler.com › topics › javascript › null-and-undefined-in-javascript
Null and Undefined in JavaScript - Scaler Topics
April 21, 2022 - Basically, null is a primitive type of a variable in JavaScript. Many people consider it a bug in JavaScript as it considers null as an object. Changing or fixing this bug will break the existing codebase, so it is not yet changed. The type of null is an object.
🌐
Reddit
reddit.com › r/learnjavascript › "typeof" null an object?
r/learnjavascript on Reddit: "typeof" null an object?
November 26, 2022 -

We know that the "typeof" null in javascript is an object, and we also know that it's a bug in JavaScript. I wanted to know what kind of bug it is, and why anyone hasn't resolved it yet.

🌐
GitHub
github.com › rohan-paul › Awesome-JavaScript-Interviews › blob › master › Javascript › Tricky-JS-Problems › typeof-null-why-its-object.md
Awesome-JavaScript-Interviews/Javascript/Tricky-JS-Problems/typeof-null-why-its-object.md at master · rohan-paul/Awesome-JavaScript-Interviews
In fact, the ECMAScript specification defines null as the primitive value that represents the intentional absence of any object value (ECMA-262, 11.4.11). To draw a parallel here, consider typeof(NaN) === "number".
Author   rohan-paul
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
The rest of this page applies for when strictNullChecks is enabled. null and undefined are primitive types and can be used like other types, such as string. let value: string | undefined | null = null; value = 'hello'; value = undefined; Try ...
🌐
Wes Bos
wesbos.com › javascript › 01-the-basics › types-null-and-undefined
Types - Null and Undefined - Wes Bos
With the dog example we used above, we have created the variable but we have not set a value. That is the difference. Now we will discuss the null type. Null is a value of nothing, whereas undefined is a variable that has not yet had a value ...
🌐
YouTube
youtube.com › watch
Javascript null data type explained | JS for Beginners Ep. 14 - YouTube
The null data type in JavaScript is a primitive value that is used to indicate the absence of an object or data. In this beginner tutorial, we look at exampl...
Published   March 2, 2019