Home >>JavaScript Array Reference >JavaScript Array toString() Method

JavaScript Array toString() Method

JavaScript Array toString() Method

JavaScript toString() method is used to return a string with all the values of the given input array separated by commas. It does not change the original given array.

Syntax:
array.toString()

Browser Support

Method Chrome Edge Firefox Safari Opera
toString() Yes Yes Yes Yes Yes
Here is an example of JavaScript toString() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() 
{
  var a = ["A", "B", "C", "D", "E", "F", "G", "H"];
  var x = a.toString();
  document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() 
{
  var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  var x = a.toString();
  document.getElementById("demo1").innerHTML = x;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.


No Sidebar ads