Why null
is an object in JavaScript, but null instanceof Object
returns false
? Is it because of the different constructors?
Asked
Active
Viewed 240 times
-1

Ivar
- 6,138
- 12
- 49
- 61

kpkawaii pkkawaii
- 37
- 6
-
Do you have an example you would like us to take a look at in context? – ed2 Aug 27 '20 at 11:52
-
3Who said that? null is a primitive value in Javascript. – jperl Aug 27 '20 at 11:52
-
5Does this answer your question? [Javascript confusing syntax inconsistence for null, instanceof and typeof?](https://stackoverflow.com/questions/28956060/javascript-confusing-syntax-inconsistence-for-null-instanceof-and-typeof) – Sudhir Ojha Aug 27 '20 at 11:52
-
The answer to "why" is always "because that's how the specification is written". – Heretic Monkey Aug 27 '20 at 11:53
-
Related: [Why is typeof null “object”?](https://stackoverflow.com/questions/18808226/why-is-typeof-null-object) – Ivar Aug 27 '20 at 12:00
1 Answers
0
null
is not an object in JavaScript, it is a primitive type. There is a known that typeof null === 'object'
but still, null
counts as a primitive and instanceof
returns false
for all primitives.

Boussadjra Brahim
- 82,684
- 19
- 144
- 164

Anton
- 440
- 3
- 10
-
2
-
3It's not a bug. `typeof null` is defined to return "object". It would be a bug if an implementation returned anything other than "object". – Heretic Monkey Aug 27 '20 at 11:57