To turn JavaScript array into a comma-separated list, use the Array.join() method. JavaScript array join() method joins all the elements of an array into a string. The following is the syntax −
Syntax
array.join(separator);
The following is the parameter −
- separator − Specifies a string to separate each element of the array. If omitted, the array elements are separated by a comma.
Example
You can try to run the following code to turn JavaScript array into a comma-separated list −
<html> <head> <title>JavaScript Array join Method</title> </head> <body> <script> var arr = new Array("Java","PHP","Ruby"); var str = arr.join(); document.write("str : " + str ); var str = arr.join(", "); document.write("<br />str : " + str ); </script> </body> </html>