JS: Object.prototype.toString

By Xah Lee. Date: . Last updated: .

Object.prototype.toString ()

Return a string representation of the object in thisBinding.

[see JS: “this” Binding]

Usually used like this:

Object.prototype.toString.call ( obj )

If obj is a object, and has a symbol key property Symbol.toStringTag, either own property or inherited, and its value is a string, let's say the value is tag, then the result is "[object tag]"

Else, the result is one of the following:

Often used to find out the “type of object”. For example, {} is a data object, but /abc/ is regexp object.

[see JS: Determine Type of Object]

console.log (
Object.prototype.toString.call ( {} ) === "[object Object]"
); // true

console.log (
Object.prototype.toString.call ( [] ) === "[object Array]"
); // true

console.log (
Object.prototype.toString.call ( /./ ) === "[object RegExp]"
); // true

console.log (
Object.prototype.toString.call ( new Date() ) === "[object Date]"
); // true

console.log (
Object.prototype.toString.call ( function f() {} ) === "[object Function]"
); // true

console.log (
Object.prototype.toString.call ( x => 3 ) === "[object Function]"
); // true

console.log (
Object.prototype.toString.call ( 1 ) === "[object Number]"
); // true

console.log (
Object.prototype.toString.call ( "" ) === "[object String]"
); // true

console.log (
Object.prototype.toString.call ( true ) === "[object Boolean]"
); // true

Object.prototype.toString.call ( new Set() ) === "[object Set]"); // true

Object.prototype.toString.call ( new Map() ) === "[object Map]"); // true

Example of object that has property key Symbol.toStringTag and value is a string.

let uu = {};
uu[Symbol.toStringTag] = "xyz";
console.log( Object.prototype.toString.call ( uu ) === "[object xyz]"); // true

[see JS: Symbol Tutorial]

[see JS: Symbol Object]

History of Object.prototype.toString

JavaScript language by spec does not have the concept of “different type of objects” at the language level.

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]

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

In ECMAScript spec 5.1 (2011), object has a internal slot denoted “[[Class]]”.

ECMAScript 5.1 §8#sec-8.6.2

Its value is a string, representing the classification of the object.

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

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

ECMAScript 5.1 §15#sec-

The possible return values are:

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

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

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

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 and Map. [see JS: Set Object]. [see JS: 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® 2016 Language Specification#sec-object.prototype.tostring

JS String

  1. String Overview
  2. Template String
  3. Char, Code Unit, Code Point
  4. String Escape Sequence
  5. Unicode Escape Sequence
  6. String to Number
  7. Encode URL, Escape String
  8. Format Number
  9. Source Code Encoding
  10. Allowed Characters in Identifier
  11. String Object
  12. String.prototype

JS Object and Inheritance

  1. Object Overview
  2. Object Type
  3. Prototype and Inheritance
  4. Create Object
  5. Object Literal Expression
  6. Create Object with Parent X
  7. Get/Set Prototype
  8. Prevent Adding Property
  9. Determine Type of Object
  10. Clone Object
  11. Test Object Equality
  12. Add Method to Prototype
  13. Object Object
  14. Object.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog