The Javascript Array toString()
method
converts an array to a String.
The returned string value is the elements in the array with commas.
toString() |
Yes | Yes | Yes | Yes | Yes |
array.toString()
None
It returns a string value representing the values in the array separated by a comma.
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.
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: