.toString()

Anonymous contributor's avatar'
Anonymous contributor
Anonymous contributor's avatar'
Anonymous contributor
Published Oct 31, 2022
Contribute to Docs

The .toString() method returns a string representation of a given value or object.

Syntax

stringObject.toString()

The .toString() method takes no parameters.

Note: This method can be called on numbers, arrays, and objects.

Example

The following example demonstrates the .toString() method:

// Assigning constant to integer value
const myNum = 3;
// Using .toString() method to get its string value
const str = myNum.toString();
console.log('Value of str variable is', str);
// Using typeof operator to show string value was returned
console.log('Type of str variable is', typeof str);

This will produce the following output:

Value of str variable is 3
Type of str variable is string

All contributors

Looking to contribute?

Learn JavaScript on Codecademy