The Wayback Machine - https://web.archive.org/web/20250402211016/https://www.geeksforgeeks.org/javascript-array-tostring-method/
Open In App

JavaScript Array toString() Method

Last Updated : 14 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The toString() method returns a string with array values separated by commas. The toString() method does not change the original array. This method converts an array into a readable string format, often for display or logging purposes.

Syntax:

arr.toString()

Parameters: This method does not accept any parameter.

Return value: 

  • The method returns the string representation of the array elements. 
  • If the array is empty, then it returns an empty string.
  • The original array is not changed by this method

Below is an example of the Array toString() method.

Example 1: In this example The func() function creates an array and converts it to a string using the toString() method.

JavaScript
// JavaScript to illustrate toString() method
function func() {

    // Original array
    let arr = ["Geeks", "for", "Geeks"];

    // Creating a string
    let str = arr.toString();
    console.log(str);
}
func();

Output
Geeks,for,Geeks

Example 2: In this example, the toString() method creates a string consisting of array elements.

JavaScript
// JavaScript to illustrate toString() method
function func() {
    // Original array
    let arr = [2, 5, 8, 1, 4];

    // Creating a string
    let str = arr.toString();
    console.log(str);
}
func()

Output
2,5,8,1,4

Example 3 : In this example, we are converting a multidimensional array into a string.

JavaScript
let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

let result = matrix.toString();
console.log(result);

Output
1,2,3,4,5,6,7,8,9

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers: The browsers supported by JavaScript Array toString() method are listed below:

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

JavaScript Array toString() Method – FAQs

How do you use the Array.toString() method?

To use the Array.toString() method, call it on an array. For example:

let array = [1, 2, 3];
let result = array.toString();
console.log(result); // Output: "1,2,3"

What is the return value of the Array.toString() method?

The Array.toString() method returns a string representing the elements of the array, separated by commas.

Does the Array.toString() method affect the original array?

No, the Array.toString() method does not modify the original array. It only returns a string representation of the array.

Can the Array.toString() method handle nested arrays?

Yes, the Array.toString() method can handle nested arrays. When converting nested arrays, it will include the nested elements as part of the string. For example:

let array = [1, [2, 3], 4];
let result = array.toString();
console.log(result); // Output: "1,2,3,4"

How does Array.toString() differ from Array.join()?

The Array.toString() method is similar to Array.join(), but Array.join() allows you to specify a custom separator. By default, Array.toString() uses a comma as the separator. For example:

let array = [1, 2, 3];
let resultToString = array.toString();
let resultJoin = array.join('-');
console.log(resultToString); // Output: "1,2,3"
console.log(resultJoin); // Output: "1-2-3"


Next Article

Similar Reads

three90RightbarBannerImg