History of Object.prototype.toString

By Xah Lee. Date: . Last updated: .

History of Object.prototype.toString

JavaScript language does not have the concept of “type of object”.

The typeof operator on Array, RegExp, Date, JSON, etc all just return "object".

The only exception is function, which typeof a_function returns "function".

see JS: Value Types

This inconsistency is due to bad design.

However, JavaScript has some internal way to determine what type of object it is.

Object has a internal slot denoted “[[Class]]”. Its value is a string, representing the classification of the object.

This [[Class]] is what we think of the “type of object”.

ECMAScript 5.1 2026-11-15 238cb L
ECMAScript 5.1 2026-11-15 238cb L

Object.prototype.toString() return the value of internal slot [[class]], in this string form "[object [[Class]]]"

ECMAScript 5.1 2026-11-15 23991 L
ECMAScript 5.1 2026-11-15 23991 L

The possible return values are:

etc

But in in ECMAScript 2015 , the internal slot “[[Class]]” is removed from the spec. (probably because to avoid confusion with new ES2015 class feature. see JS: Define class (constructor). )

However, the behavior of Object.prototype.toString() is kept for backward compatibility, but how it works has changed.

Now, if the object has a symbol property key Symbol.toStringTag, then that is used as part of the return value. Else, it falls back to ES5 behavior as is.

ECMAScript 2015 added new kinds of objects, such as Set Object and Map Object . New object's prototypes typically have symbol properties Symbol.toStringTag. For example, for set object, the value is the string "Set".

The symbol property Symbol.toStringTag can be set by user.

ECMAScript 2015 added this note:

NOTE Historically, this function was occasionally used to access the String value of the [[Class]] internal slot that was used in previous editions of this specification as a nominal type tag for various built-in objects. The above definition of toString preserves compatibility for legacy code that uses toString as a test for those specific kinds of built-in objects. It does not provide a reliable type testing mechanism for other kinds of built-in or program defined objects. In addition, programs can use @@toStringTag in ways that will invalidate the reliability of such legacy type tests.

ECMAScript 2015 §Fundamental Objects#sec-object.prototype.tostring

ECMAScript 2015 2026-11-15 24a55 L
ECMAScript 2015 2026-11-15 24a55 L