toString() MethodThe 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.
toString() returns a new string; it does not modify the original value.toString().toString() method on your own objects.toString() directly on null or undefined causes an error (they don’t have this method).JSON.stringify().The basic syntax for converting a value to a string is:
value.toString()
For numbers, you can also pass a radix (base) to get different numeral systems (binary, hex, etc.):
number.toString(radix)
// Examples:
(15).toString(2); // "1111" (binary)
(255).toString(16); // "ff" (hexadecimal)
// 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
toString()
let person = {
name: "John",
age: 30,
toString: function() {
return this.name + " (" + this.age + ")";
}
};
console.log(person.toString()); // "John (30)"
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."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.
toString() does not change the original variable; it always returns a new string.toString() when you specifically want a string version of a value for display or logging.toString() directly on null or undefined — check the value first.JSON.stringify() instead of toString().toString() to your objects to make debugging and logging easier.toString() and verify their types with typeof.toString() on an array and compare the result with JSON.stringify().toString() method and log it to the console.toString() on null inside a try...catch block and observe the error.