JavaScript object toString() method
- Previous page seal()
- Next page valueOf()
- Go up one level JavaScript object reference manual
Definition and usage
toString()
The method returns the object as a string.
If toString()
The method cannot return a string and returns "[object Object]" instead.
Object.toString()
Always returns the object constructor.
toString()
The method does not change the original object.
Description
Every JavaScript object has toString()
Method.
When JavaScript needs to display an object as text (such as in HTML) or use an object as a string internally, toString()
Method.
Normally, you would not use it in your own code.
Instance
Example 1
Using toString() on an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"]; let text = fruits.toString();
Example 2
Using toString() on an object:
const person = { firstName: "Bill", lastName: "Gates", age: 19, eyeColor: "blue" }; const keys = person.toString();
Example 3
Using Object.toString() on an object:
const person = { firstName: "Bill", lastName: "Gates", age: 19, eyeColor: "blue" }; const keys = Object.toString(person);
Syntax
object.toString()
Parameter
No parameters.
Return value
Type | Description |
---|---|
String | Represents the string of the object. |
"[object type]" | If it fails to return a string. |
Browser support
toString()
Is ECMAScript1 (ES1) feature.
All modern browsers support ES1 (JavaScript 1997):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
Related pages
- Previous page seal()
- Next page valueOf()
- Go up one level JavaScript object reference manual