← Back to Chapters

JavaScript toString() Method

? JavaScript toString() Method

⚡ Quick Overview

The JavaScript toString() method converts a value (number, boolean, array, date, or object) into its string representation. It exists on most built-in types and is automatically available through JavaScript’s prototype system.

? Key Concepts

  • toString() returns a new string; it does not modify the original value.
  • Most primitive wrapper objects (Number, Boolean, String, Date, Array, Object) implement toString().
  • Arrays join their elements with commas when converted to a string.
  • You can define a custom toString() method on your own objects.
  • Calling toString() directly on null or undefined causes an error (they don’t have this method).
  • For structured data (like objects/arrays you want as JSON), prefer JSON.stringify().

? Syntax

The basic syntax for converting a value to a string is:

? View Syntax
value.toString()

For numbers, you can also pass a radix (base) to get different numeral systems (binary, hex, etc.):

? Number with Radix
number.toString(radix)
// Examples:
(15).toString(2);   // "1111"  (binary)
(255).toString(16); // "ff"    (hexadecimal)

? Code Examples

? Number, Boolean, Array, and Date
// Number to string
let num = 123;
let str = num.toString();
console.log(str);        // "123"
console.log(typeof str); // "string"

// Boolean to string
let flag = true;
console.log(flag.toString()); // "true"

// Array to string
let fruits = ["apple", "banana", "cherry"];
console.log(fruits.toString()); // "apple,banana,cherry"

// Date to string
let date = new Date();
console.log(date.toString()); // Converts date to readable string format
? Custom Object with toString()
let person = {
  name: "John",
  age: 30,
  toString: function() {
    return this.name + " (" + this.age + ")";
  }
};

console.log(person.toString()); // "John (30)"

? Live Output / Explanation

? What the Console Shows

For the first example, the console output will be:

  • "123" and then "string" → confirms the number became a string.
  • "true" → boolean true converted to the string "true".
  • "apple,banana,cherry" → the array joined into a comma-separated string.
  • A human-readable date → something like "Sat Nov 22 2025 18:30:00 GMT+0530 (...)".

For the custom object, instead of the default [object Object], you get a friendly string: "John (30)", because we overrode the toString() method.

? Use Cases / When to Use

  • Displaying values in the UI (e.g., in alerts, labels, or HTML elements).
  • Logging values to the console in a human-readable format.
  • Custom formatting for objects (e.g., user, product, coordinates).
  • Converting numbers to different bases (binary, hex) for debugging or low-level tasks.

? Tips & Best Practices

  • toString() does not change the original variable; it always returns a new string.
  • Use toString() when you specifically want a string version of a value for display or logging.
  • Remember that arrays use commas between elements in their string representation.
  • Do not call toString() directly on null or undefined — check the value first.
  • When you need structured JSON (especially for APIs), prefer JSON.stringify() instead of toString().
  • Consider adding a custom toString() to your objects to make debugging and logging easier.

? Try It Yourself

  • Convert a boolean and a number into strings using toString() and verify their types with typeof.
  • Call toString() on an array and compare the result with JSON.stringify().
  • Create your own object with a custom toString() method and log it to the console.
  • Try calling toString() on null inside a try...catch block and observe the error.