Javascript Reference - JavaScript Array toString() Method








The Javascript Array toString() method converts an array to a String.

The returned string value is the elements in the array with commas.

Browser Support

toString() Yes Yes Yes Yes Yes

Syntax

array.toString()

Parameters

None





Return Value

It returns a string value representing the values in the array separated by a comma.

Example


var colors = ["red", "blue", "green"]; 
    
console.log(colors.toString()); //red,blue,green 
console.log(colors.valueOf()); //red,blue,green 
console.log(colors); //red,blue,green 

The code above generates the following result.

Example 2

You can click the button to convert the array into a String.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {<!--  w  w  w  .  j a v  a 2 s.  c  o  m-->
    var array1 = ["A", "B", "C", "D"];
    array1.toString();
    document.getElementById("demo").innerHTML = array1;
}
</script>

</body>
</html>

The code above is rendered as follows: