The toString()
method returns a string representing the object.
The toString()
method returns a string representing the object.
toString()
By default toString()
takes no parameters. However, objects that inherit from Object
may override it with their own implementations that do take parameters. For example, the Number.prototype.toString()
and BigInt.prototype.toString()
methods take an optional radix
parameter.
A string representing the object.
An object's toString()
method is most commonly invoked when that object undergoes:
String
(myObject)
)myObject + "hello world"
)Note: This assumes the object does not have a custom implementation of Symbol.toPrimitive
. If it does, that method will take priority and be called instead of toString()
.
While not as common, the method can be invoked directly (for example, myObject.toString()
).
This method is inherited by every object descended from Object
, but can be overridden by descendant objects (for example, Number.prototype.toString()
). To use the base Object.prototype.toString()
with an object that has it overridden (or to invoke it on null
or undefined
), you need to call Function.prototype.call()
or Function.prototype.apply()
on it, passing the object you want to inspect as the first parameter (called thisArg
).
const arr = [1, 2, 3]; arr.toString() // "1,2,3" Object.prototype.toString.call(arr) // "[object Array]"
Object.prototype.toString()
returns "[object Type]"
, where Type
is the object type. If the object has a Symbol.toStringTag
property whose value is a string, that value will be used as the Type
. Many built-in objects, including Map
and Symbol
, have a Symbol.toStringTag
. Some objects predating ES6 do not have Symbol.toStringTag
, but have a special tag nonetheless. They include (the tag is the same as the type name given below):
The arguments
object returns "[object Arguments]"
. Everything else, including user-defined classes, unless with a custom Symbol.toStringTag
, will return "[object Object]"
.
Object.prototype.toString()
invoked on null
and undefined
returns [object Null]
and [object Undefined]
, respectively.
You can create a function to be called in place of the default toString()
method. The toString()
function you create must return a primitive. If it returns an object and the method is called implicitly (i.e. during type conversion or coercion), then its result will be ignored and the value of a related method,
, will be used instead, or a valueOf()
TypeError
will be thrown if none of these methods return a primitive.
The following code defines the Dog
object type and creates theDog
, an object of type Dog
:
function Dog(name, breed, color, sex) { this.name = name; this.breed = breed; this.color = color; this.sex = sex; } const theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');
If you call the toString()
method on this custom object, it returns the default value inherited from Object
:
theDog.toString(); // returns [object Object]
The following code creates and assigns dogToString()
to override the default toString()
method. This function generates a string containing the name
, breed
, color
, and sex
of the object.
Dog.prototype.toString = function dogToString() { return 'Dog ' + this.name + ' is a ' + this.sex + ' ' + this.color + ' ' + this.breed; }
Or, using template strings:
Dog.prototype.toString = function dogToString() { return `Dog ${this.name} is a ${this.sex}${this.color}${this.breed}`; }
With the preceding code in place, any time toString()
is used in a Dog
context, JavaScript automatically calls the dogToString()
function, which returns the following string:
"Dog Gabby is a female chocolate Lab"
toString()
can be used with every object and (by default) allows you to get its class.
const toString = Object.prototype.toString; toString.call(new Date); // [object Date] toString.call(new String); // [object String] // Math has its Symbol.toStringTag toString.call(Math); // [object Math] toString.call(undefined); // [object Undefined] toString.call(null); // [object Null]
Using toString()
in this way is unreliable; objects can change the behavior of Object.prototype.toString()
by defining a Symbol.toStringTag
property, leading to unexpected results. For example:
const myDate = new Date(); Object.prototype.toString.call(myDate); // [object Date] myDate[Symbol.toStringTag] = 'myDate'; Object.prototype.toString.call(myDate); // [object myDate] Date.prototype[Symbol.toStringTag] = 'prototype polluted'; Object.prototype.toString.call(new Date()); // [object prototype polluted]
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
toString |
1 |
12 |
1 |
3 |
3 |
1 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString