loxxy
loxxy

Reputation: 13151

Are [object Null] & [object Undefined] yet to be implemented?

While reading MDN here, I came across

Starting in JavaScript 1.8.5 toString() called on null returns [object Null], and undefined returns [object Undefined], as defined in the 5th Edition of ECMAScript and a subsequent Errata. See Using toString to detect object type.

I tried Object(null).toString() & Object(undefined).toString()

and both returned "[object Object]"

As per the specification, primitive types in JS are Undefined, Null, Boolean, Number, or String.

So would it be correct to assume that, at the moment, all the browsers are yet to implement:

[object Undefined] & [object Null] ?

Upvotes: 2

Views: 85

Answers (1)

user2437417
user2437417

Reputation:

To get the internal [[Class]], you need to set the value as the this value of Object.prototype.toString(), so:

Object.prototype.toString.call(null);      // [object Null]
Object.prototype.toString.call(undefined); // [object Undefined]

This is defined in ECMAScript 5 as follows:

8.6.2 Object Internal Properties and Methods

The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).

Upvotes: 2

Related Questions