JavaScript Array toString() | Get a String from an Array

The JavaScript toString() method is used to get a string from a specified array. The string contains its value as all elements of specified array separated by commas. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="xyz"></p>

   <script>
      const myArray = ["Seattle", "Oakland", "Boston", "Austin", "Denver", "Berlin"];
      let myString = myArray.toString();
      document.getElementById("xyz").innerHTML = myString;
   </script>
   
</body>
</html>
Output

JavaScript toString() Syntax

The syntax of toString() method in JavaScript is:

array.toString()

JavaScript toString() Example

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p>Value of <b>y</b> = <span id="a"></span></p>
   <p>Type of <b>y</b> = <span id="b"></span></p>

   <script>
      let x = ["Seattle", "Oakland", "Boston", "Austin", "Denver", "Berlin"];

      let y = x.toString();
      
      document.getElementById("a").innerHTML = y;
      document.getElementById("b").innerHTML = typeof(y);
   </script>
   
</body>
</html>
Output

Value of y =

Type of y =

JavaScript Online Test


« Previous Tutorial Next Tutorial »