How to convert JavaScript array into the comma-separated string?

How to convert JavaScript array into the comma-separated string?

In this Javascript tutorial, I am going to let you know how to create comma separated list from an array in Javascript.

In the Javascript, there are many method that converts array into string and sometime you need specified separator while converting array into the string.

The following example will let you know how you can define separator and how many ways you can convert array into the string list.

Array join() method

You can use join method that will join the array elements into the new string. By default array elements are separated with a (",") comma separator.

Syntax:
array.join()

You can also pass the separator in join method to convert array with specified separator.

array.join(", ")
Example
<html>
   <head>
      <title>JavaScript - Convert array into the comma-separated string - Expertphp.in</title>
   </head>

   <body>
      <script>
         var arr = ["Web Developer","Web Designer","Tester"];         
         // with default separator
         document.write(arr.join());
         // with defined separator
         document.write(arr.join("="));
      </script>
   </body>
</html>
Array toString() method

You can also use toString method to convert array into a string. It will return a string, separated by a comma.

Syntax:
array.toString()
Example
<html>
   <head>
      <title>JavaScript - Convert array into the comma-separated string - Expertphp.in</title>
   </head>

   <body>
      <script>
         var arr = ["Web Developer","Web Designer","Tester"];                 
         document.write(arr.toString());
      </script>
   </body>
</html>

Phone: (+91) 8800417876
Noida, 201301