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
Like undefined, accessing any property on null throws a TypeError instead of returning undefined or searching prototype chains. Like undefined, null is treated as falsy for boolean operations, and nullish for nullish coalescing and optional chaining. The typeof null result is "object". This is a bug in JavaScript that cannot be fixed due to backward compatibility.
🌐
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.
🌐
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.
🌐
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".
🌐
W3Schools
w3schools.com › js › js_datatypes.asp
JavaScript Data Types
Booleans are often used in conditional testing. ... JavaScript Objects represent complex data structures and functionalities beyond the primitive data types (string, number, boolean, null, undefined, symbol, bigint).
🌐
freeCodeCamp
freecodecamp.org › news › how-to-check-for-null-in-javascript
JS Check for Null – Null Checking in JavaScript Explained
November 7, 2024 - Null and undefined are very similar in JavaScript and are both primitive types. A variable has the type of null if it intentionally contains the value of null.
Find elsewhere
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
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › null
`null` in JavaScript - Mastering JS
In JavaScript, null is a value that represents the intentional absence of any object value. It is technically a primitive type, although in some cases it behaves as an object.
🌐
DEV Community
dev.to › elmarshall › javascript-typeof-null-52k0
JavaScript: typeof null - DEV Community
May 29, 2020 - typeof returns the value type, and === strictly evaluates equality. x is null, so we'd expect to get null, and true. Logical. Unfortunately, what you actually get is object, true. That's right, null is an object... But doesn't that seem off? Well, this is due to a quirk of the way null was defined in the early days of JavaScript, and can’t be altered at this point.
🌐
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". So why does JavaScript give "number" as the type of NaN (not a number)?
Author   rohan-paul
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-null
Everything about null in JavaScript
null, alongside false, 0, '', undefined, NaN, is a falsy value. If a falsy value is encountered in conditionals, then JavaScript coerces falsy to false. ... typeof value operator determines the type of value.
🌐
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.
🌐
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 ...
🌐
2ality
2ality.com › 2013 › 10 › typeof-null.html
The history of “typeof null”
Update 2013-11-05: I take a look at the C code of typeof to better explain why typeof null results in 'object'. 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).
🌐
DEV Community
dev.to › _ravo_lution › why-typeof-null-is-object-181
Why typeof(null) is "object"? - DEV Community
July 26, 2021 - Or it was zero, then the type tag was three bits in length, providing two additional bits, for four types. ... null (JSVAL_NULL) was the machine code NULL pointer. Or: an object type tag plus a reference that is zero.
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-null-and-undefined
Difference between null and undefined in JavaScript
The type of null variable is object whereas the type of undefined variable is "undefined". ... let num1 = null; let num2; console.log(typeof num1);//"object" console.log(typeof num2); //"undefined" Use the === operator to check whether a variable ...
🌐
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.