Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is null an object in JavaScript? [duplicate]

Possible Duplicate:
Null object in javascript

Hi, I've read this thread about null in JavaScript, but I'm very confused about the identity of null now.

As it is known that typeof(null) evaluates to object because of the language design error, and ECMA states that null is The Null Type.

   8.2 The Null Type
   The Null type has exactly one value, called null.

So why people keep saying that null is an object?

Someone said null is a singleton object. Is that how everybody sees the null as in JavaScript too?

like image 956
c4il Avatar asked Sep 07 '10 21:09

c4il


People also ask

IS null considered an object in JavaScript?

null is not an object, it is a primitive value. For example, you cannot add properties to it. Sometimes people wrongly assume that it is an object, because typeof null returns "object" . But that is actually a bug (that might even be fixed in ECMAScript 6).

Why null in JavaScript is object?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

How does null work in JavaScript?

'Null' is a keyword in JavaScript that signifies 'no value' or nonexistence of any value. If you wish to shred a variable off its assigned value, you can simply assign 'null' to it. Besides this, like any other object, it is never implicitly assigned to a variable by JavaScript.

Is Empty object Falsy JavaScript?

There are only seven values that are falsy in JavaScript, and empty objects are not one of them. An empty object is an object that has no properties of its own. You can use the Object. keys() function to check if an object is empty as shown below.


2 Answers

Null is the absence of an object. Undefined means it hasn't been assigned yet, and null means it has been assigned to be nothing.

Null is not really a singleton object, because dereferencing it will cause an error; for (var x in null) will give you an error. Think back to the pointer days; null was the value a pointer had when it was not pointing to an object.

like image 89
Anthony Mills Avatar answered Sep 29 '22 05:09

Anthony Mills


No, null is one of the few primitive types (others being numbers, strings, booleans, and undefined). Everything else is an object, including functions, arrays and regular expressions. Numbers, strings and booleans are often called "object-like", because they have methods, but they are immutable. Objects on the other hand are mutable.

like image 42
Daniel Vassallo Avatar answered Sep 29 '22 07:09

Daniel Vassallo