JavaScript String toString()

Definition and usage

toString() The method returns the string as a string.

toString() The method does not change the original string.

toString() The method can be used to convert a string object to a string.

See also:

valueOf() method

Hint

Each JavaScript object has toString() Method.

If you need to display an object as text (such as in HTML) or use an object as a string, JavaScript uses internally toString() Method.

Generally, you will not use it in your own code.

Instance

Example 1

Get the text content:

let text = "Hello World!";
let result = text.toString();

Try it yourself

It is equivalent to:

let text = "Hello World!";
let result = text;

Try it yourself

Example 2

Get the value of the String object:

let text = new String("Hello World!");
let result = text.toString();

Try it yourself

Syntax

string.toString()

Parameter

No parameters.

Return value

Type Description
String The content of the string.

thrown

Exception Description
TypeError Exception thrown when the object called by the method is not a String.

browser support

toString() Adalah ciri ECMAScript1 (ES1).

Semua pelayar mengesahkan ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Dukungan Dukungan Dukungan Dukungan Dukungan Dukungan

Halaman Berkenaan

String JavaScript

Kemudahan String JavaScript

Pencarian String JavaScript