Join Elements of String Array with Comma

To join elements of a string array with comma as separator in JavaScript, call join() method on the string array and pass the comma (separator string) as argument in the method call.

The expression to join elements of a string array arr by a comma ',' is

</>
Copy
arr.join(',');

Example

In the following example, we take a string array in arr, join the elements in the array with comma character ','as separator, and display the resulting string in pre#output.

index.html

<!DOCTYPE html>
<html lang="en">
  <body>
    <pre id="output">Output : </pre>
    <script>
      var arr = ['apple', 'banana', 'cherry'];
      var strOutput = arr.join(',');
      document.getElementById('output').innerHTML += strOutput;
    </script>
  </body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to join the given string array by comma character using Array.join() method, with example program.