Array.prototype.toString() returns a string representation of an array. For normal arrays it behaves like join() with a comma separator.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
console.log(scripts.toString());
// PHP,ASP,JavaScript,HTML
const values = [10, 20, 30];
const text = values.toString();
console.log(text); // 10,20,30
console.log(typeof text); // string
The method takes no separator argument. If you need control over the separator, use join().
const values = ["A", "B", "C"];
console.log(values.toString()); // A,B,C
console.log(values.join(" - ")); // A - B - C
Array toString() uses the array's join() behavior internally in ordinary cases.
const matrix = [[1, 2], [3, 4]];
console.log(matrix.toString()); // 1,2,3,4
The result can look flattened because nested arrays are recursively converted to comma-separated strings. Structure such as row boundaries is not preserved.
const values = ["A", null, undefined, , "E"];
console.log(values.toString()); // A,,,,E
These positions become empty text between commas in the array string representation.
const values = [1, 2, 3];
console.log("Values: " + values); // Values: 1,2,3
console.log(`${values}`); // 1,2,3
Explicit conversion is usually clearer when the output format matters.
const matrix = [[1, 2], [3, 4]];
console.log(matrix.toString()); // 1,2,3,4
console.log(JSON.stringify(matrix)); // [[1,2],[3,4]]
JSON.stringify() preserves JSON-compatible array/object structure, while toString() is primarily a simple textual representation.
The original page also taught number conversion. Number.prototype.toString() can accept a radix from 2 through 36, which is different from array toString().
const value = 13;
console.log(value.toString()); // "13"
console.log(value.toString(2)); // "1101"
console.log(value.toString(8)); // "15"
console.log(value.toString(16)); // "d"
To convert strings back to integers, see parseInt().
toString(); use join().toString() as a structured serialization format.toString() with number toString(radix).Custom Separators with join() Creating Arrays Display Array Elements
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.