JavaScript Array toString() Method

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

Convert an Array to a String Top ↑

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().

Array toString() vs join() Top ↑

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.

toString() with Nested Arrays Top ↑

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.

null, undefined, and Empty Slots Top ↑

const values = ["A", null, undefined, , "E"];
console.log(values.toString()); // A,,,,E

These positions become empty text between commas in the array string representation.

JavaScript May Convert Arrays to Strings Automatically Top ↑

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.

Use JSON.stringify() When You Need Structure Top ↑

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.

Number toString(radix) Is a Different Method Top ↑

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().

Common toString() Mistakes Top ↑

  • Passing a custom separator to array toString(); use join().
  • Using array toString() as a structured serialization format.
  • Confusing array toString() with number toString(radix).
  • Assuming nested arrays keep their row/column boundaries in the resulting text.

Custom Separators with join() Creating Arrays Display Array Elements




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer