Open In App

JavaScript Array toString() Method

The JavaScript Array toString() Method returns the string representation of the array elements

Syntax:



arr.toString()

Parameters: This method does not accept any parameter.

Return value: 



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

Example 1: 




// 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 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.




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.


Article Tags :