JavaScript: Object.prototype.toString

By Xah Lee. Date: . Last updated: .

Object.prototype.toString ()

Returns a string representation of the object in thisBinding.

〔►see JavaScript: Keyword “this”

Usually used like this: ( 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 object object, but /abc/ is regexp object.

〔►see JavaScript: Determine Type of Object

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

console.log ( ( [] ) === "[object Array]"
); // true

console.log ( ( /./ ) === "[object RegExp]"
); // true

console.log ( ( new Date() ) === "[object Date]"
); // true

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

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

console.log ( ( 1 ) === "[object Number]"
); // true

console.log ( ( "" ) === "[object String]"
); // true

console.log ( ( true ) === "[object Boolean]"
); // true

console.log( ( new Set() ) === "[object Set]"); // true

console.log( ( 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( ( uu ) === "[object xyz]"); // true

〔►see JavaScript: Symbol Tutorial

〔►see JavaScript: Symbol Object

History of Object.prototype.toString

JavaScript language spec does not technically have different type of objects as different data types distinguisable by the typeof operator. Array, RegExp, Date, JSON, etc instances are all just object type. 〔►see JavaScript: Data Types

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

The function Object.prototype.toString() returns 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 JavaScript: Class Tutorial〕 )

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 JavaScript: Set Object〕. 〔►see JavaScript: 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

String Topic

  1. JavaScript: Default Charset/Encoding
  2. JavaScript: String is 16-Bit Unit Sequence
  3. JavaScript: Unicode Character Escape Sequence
  4. JavaScript: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML XML Entities

  1. JavaScript: Template String
  2. JavaScript: Convert String to Number
  3. JavaScript Encode URL, Escape String
  4. JavaScript: Format Number
  5. JavaScript: JSON Object

  1. JavaScript: String Object
  2. JavaScript: String.prototype
  3. JavaScript: String Constructor
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.