Create a Comma Separated List from an Array in JavaScript
Last Updated :
28 Jun, 2025
Creating a comma-separated list from an array is a common task in JavaScript, especially when formatting data for display, creating CSV strings, or sending readable outputs to users or servers. Over 75% of JavaScript applications use some form of array-to-string conversion for logging, UI rendering, or data transmission. JavaScript provides simple and best methods like .join()
to seamlessly transform arrays into well-formatted, comma-separated strings.
Here are the different methods to create a comma-separated list from an Array
1. Array join() method
The Array join() method joins the elements of the array to form a string and returns the new string. The elements of the array will be separated by a specified separator.
index.js
let a = ["GFG1", "GFG2", "GFG3"];
function fun() {
console.log("Comma Separates List: " + a.join(", "));
}
fun();
Output
outputCode Overview
- The code defines an array containing three elements:
"GFG1"
, "GFG2"
, and "GFG3"
. - The function uses
a.join(", ")
to create a comma-separated list of the array elements and log it to the console.
2. Array toString() method
Array toString() method converts an array into a String and returns the new string. It returns a string that represents the values of the array, separated by a comma.
index.js
let a = ["GFG1", "GFG2", "GFG3"];
function fun() {
console.log("Comma Separated List: " + a.toString());
}
fun();
Output
outputCode Overview
- The code defines an array with elements "GFG1", "GFG2", and "GFG3".
- The function uses the toString() method to convert the array into a string, with the elements separated by commas by default.
3. Using Array literals
Array literals is the list of expressions containing array elements separated with commas enclosed with square brackets.
index.js
let a = ["GFG1", "GFG2", "GFG3"];
function fun() {
console.log("Comma Separated List: " + a);
}
fun();
Output
outputCode Overview
- The code defines an array with elements "GFG1", "GFG2", and "GFG3" using an array literal.
- The function directly logs the array arr to the console. When an array is logged as a string, JavaScript automatically converts it to a comma-separated list.
4. Using Lodash _.join() method
Lodash _.join() method return the list of given array with "," separation.
index.js
// Requiring the lodash library
let _ = require("lodash");
let a = [1, 2, 3, 4, 5, 6];
let newA = _.join(a);
console.log("After Join: " + newA);
Output
outputCode Overview
- The code imports the lodash library using require("lodash"), enabling access to its utility functions.
- The _.join(array) function from Lodash is used to join the elements of the array into a string, with the default separator being a comma.
5. Using map() and join()
To create a comma-separated list from an array using map() and join(), first convert each array element to a string using map(String), then join these strings into a single string separated by commas using join(',').
index.js
const a = [1, 2, 3, 4, 5];
const comma = a.map(String).join(',');
console.log(comma);
Output
outputCode Overview
- The code uses
map(String)
to convert each element of the array arr
into a string, and then join(',')
joins these string elements with commas. - The result is a comma-separated string
"1,2,3,4,5"
, which is logged to the console.
Similar Reads
How to Create Nested Arrays from a Nest of Arrays in JavaScript ? Creating nested arrays from a nest of arrays in JavaScript involves organizing multiple arrays into a hierarchical structure, which is often useful for managing and representing complex data relationships. This process entails encapsulating arrays within other arrays to form multi-dimensional struct
3 min read
How to create HTML List from JavaScript Array? Creating an HTML list from a JavaScript array allows you to dynamically generate <ul> (unordered) or <ol> (ordered) lists and fill them with data directly from your array. This is useful when you want to display array data in a structured way on your webpage. In this guide, weâll go thro
3 min read
Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J
3 min read
JavaScript - Convert Comma Separated String To Array Here are the various methods to convert comma-separated string to array using JavaScript.1. Using the split() Method (Most Common)The split() method is the simplest and most commonly used way to convert a comma-separated string into an array. It splits a string into an array based on a specified cha
3 min read
How to convert a 2D array to a comma-separated values (CSV) string in JavaScript ? Given a 2D array, we have to convert it to a comma-separated values (CSV) string using JS. Input:[ [ "a" , "b"] , [ "c" ,"d" ] ]Output:"a,b c,d"Input:[ [ "1", "2"]["3", "4"]["5", "6"] ]Output:"1,23,45,6"To achieve this, we must know some array prototype functions which will be helpful in this regard
4 min read
How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app
5 min read