Skip to content

Array to comma separated string JavaScript | Code

  • by

Use toString() method to convert an Array to a comma-separated string in JavaScript. The method array.toString() actually calls array.join() which results in a string concatenated by commas.

array.toString(

Or pass the array as a parameter to the String object – String(arr). The String object will convert the passed-in array to a comma-separated string and return the result.

Array to comma-separated string JavaScript

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>
    var array = ['a','b','c','d','e','f'];
    console.log(array.toString());

    const arr = ['one', 'two', 'three'];
    const str = String(arr);
    console.log(str);
    console.log(typeof str);
  </script>

</body>
</html>

Output:

Array to comma separated string JavaScript

Array.prototype.join()

Or join() method joins all elements of an array into a string. It accepts a separator as an argument, but the default is already a comma ,

str = arr.join([separator = ','])

Examples:

var array = ['A', 'B', 'C'];
var myVar1 = array.join();      // 'A,B,C'
var myVar2 = array.join(', ');  // 'A, B, C'
var myVar3 = array.join(' + '); // 'A + B + C'
var myVar4 = array.join('');    // 'ABC'

Do comment if you have any doubts or suggestions on this Js Array string topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading