Why typeof (null) == 'object' in JavaScript?
It's been debated (see for example this discussion), and it's often seen as not really good, but it has a rationale : A null
value takes the place of an object, it's the value you have when you expect an object and have none.
From ECMAScript :
null : primitive value that represents the intentional absence of any object value.
It's exactly the same as NaN
being of type "number"
.
Why does typeof(null) return object , but you can't assign properties to it?
This page has a nice description of the history here surrounding why typeof(null)
gives "object":
JS Data Types - Null
Here is the relevant portion (although I would suggest you read the whole post):
Why does
typeof null
return"object"
?// What's happening here?
typeof null === "object"; // true
The answer might disappoint some, but the truth is simply because the table above says to do so.
The reasoning behind this is that
null
, in contrast withundefined
, was (and still is) often used where objects appear. In other words,null
is often used to signify an empty reference to an object. When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return"object"
. In fact, the ECMAScript specification definesnull
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)? It is because NaN
is used where numbers appear, it is a value that represents the intentional absence of a number value. Similar reasoning applies to null
, the only difference being that null
is implemented as a primitive and NaN
is actually implemented as a Number
object (so NaN.foo = 42
would actually work).
Why typeof null is 'object' in javascript?
typeof(null) returns "object"
and
typeof('') returns "string"!
Why is null an object and what's the difference between null and undefined?
(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.
Why null does not have a __proto__?
null
in JS is considered as an object. typeof null == "object" //true
As already mentioned, null
is actually a primitive value (like undefined
, a Boolean value, a String value, a Number value, or a symbol) . But the language spec dictates that typeof null
returns the string "object"
. (Similarly the specification dictates to return "function"
for function objects instead of "object"
, like it does for array objects)
But that's only one half of the story.
Then why does null not have a
__proto__
You might be wondering:
Why can I access the protoype of a string value (
"foo".__proto__
) but notnull
if both are primitive values ?
That's because Booleans, Strings and Numbers have object equivalents (e.g. new String("foo")
) and when you try to access a property on a boolean/number/string primitive, they are internally converted to an object value
console.log("foo".slice)
// does `new String("foo").slice` internally
Detecting if object is null in Javascript - why doesn't this work?
For item = null
, typeof item == "object"
is true, so inside the block you're trying to access the classList
property of a null value. You can't do that, hence the error.
To fix it, you're going to need another condition to stop null values from accessing the code.
if (typeof item == "object" && item != null)
Related Topics
Group Array of Object Nesting Some of the Keys With Specific Names
Changing the Image Source Using Jquery
How to Simulate a Click With JavaScript
Change the Url in the Browser Without Loading the New Page Using JavaScript
Curly Brackets in Arrow Functions
Read :Hover Pseudo Class with JavaScript
Preventing Child from Firing Parent's Click Event
Node.Js - How to Set Environment Variables in Code
How to Properly Reuse Connection to Mongodb Across Nodejs Application and Modules
Check Whether User Has a Chrome Extension Installed
Use of .Apply() With 'New' Operator. Is This Possible
How to Set the Caret (Cursor) Position in a Contenteditable Element (Div)
Angularjs Routing Without the Hash '#'
Read a File One Line At a Time in Node.Js
Best Way to Find If an Item Is in a JavaScript Array
Black and White Text Based on Background Image with CSS