2

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] ?

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Ohh my bad, before jumping into conclusions. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.