JavaScript toString() Method with Examples

Learn how JavaScript toString() works with numbers, arrays, booleans, and how it differs from JSON.stringify().

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

JavaScript toString() Method with Examples

toString() converts a value into a string representation. It is useful when you want text output from numbers, arrays, booleans, or objects that support string conversion.

The method is common in display logic and debugging. If you need formatted object output instead, JSON.stringify pretty print is the closer companion article.

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Convert a number to a string

Numbers become string values that you can display or concatenate.

javascript
console.log("to-string-number:", (123).toString());
Output

You should see one line logging to-string-number: 123.

Use this when a numeric value needs to become text.


Convert an array to a string

Arrays are joined with commas by default.

javascript
console.log("to-string-array:", [1, 2, 3].toString());
Output

You should see one line logging to-string-array: 1,2,3.

This is useful when you want a quick text form of a list.


Convert a boolean to a string

Boolean values become true or false text.

javascript
console.log("to-string-boolean:", true.toString());
Output

You should see one line logging to-string-boolean: true.

This is handy when the value needs to be shown or logged as text.


Summary

JavaScript toString() converts values into readable string form. Use it for numbers, arrays, and booleans when you need text output, and use JSON.stringify() when you need a more complete object representation. The method is small, but it is part of a lot of ordinary display and logging code.


Official documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …