JavaScript Array toString() Method

The JavaScript Array toString() method returns a string with array values separated by commas.

The toString() method does not change the original array.

Syntax

array.toString()

The toString() method does not have any parameters.

Return Value

It returns a string that contains all the elements of the specified array. If the array is empty, then it returns an empty string.

Examples

Below are the examples of Array toString() method.

const array1 = [1, 2, 'a', '1a'];

console.log(array1.toString());
// expected output: "1,2,a,1a"
var info = ["Terence", 28, "Kathmandu"];

var info_str = info.toString();

// toString() does not change the original array
console.log(info); // [ 'Terence', 28, 'Kathmandu' ]

// toString() returns the string representation
console.log(info_str); // Terence,28,Kathmandu

var collection = [5, null, 10, "JavaScript", NaN, [3, [14, 15]]];
console.log(collection.toString()); // 5,,10,JavaScript,NaN,3,14,15

Here, you can see that the toString() method converts all the array elements into a string and separates each element by a comma.