JavaScript Object toString()
Examples
Using toString() on an array:
let text = fruits.toString();
Using toString() on an object:
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
const keys = person.toString();
Using Object.toString() on an object:
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
const keys = Object.toString(person);
Description
The toString()
method returns an object as a string.
The toString()
method returns "[object Object]" if it cannot return a string.
Object.toString()
always returns the object constructor.
The toString()
method does not change the original object.
Note
Every JavaScript object has a toString()
method.
The toString()
method is used internally by JavaScript
when an object needs to be displayed as a text (like in HTML),
or when an object needs to be used as a string.
Browser Support
toString()
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
Syntax
Parameters
Return Value
Or "[object type]" if it cannot return a string.