Create a Comma Separated List from an Array in JavaScript Last Updated : 28 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different methods to create comma separated list from an Array1. Array join() methodThe 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. JavaScript let a = ["GFG1", "GFG2", "GFG3"]; function fun() { console.log("Comma Separates List: " + a.join(", ")); } fun(); OutputComma Separates List: GFG1, GFG2, GFG3 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 logs it to the console.2. Array toString() methodArray 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. JavaScript let a = ["GFG1", "GFG2", "GFG3"]; function fun() { console.log("Comma Separated List: " + a.toString()); } fun(); OutputComma Separated List: GFG1,GFG2,GFG3 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 literalsArray literals is the list of expressions containing array elements separated with commas enclosed with square brackets. JavaScript let a = ["GFG1", "GFG2", "GFG3"]; function fun() { console.log("Comma Separated List: " + a); } fun(); OutputComma Separated List: GFG1,GFG2,GFG3 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() methodLodash _.join() method return the list of given array with "," separation. JavaScript // Requiring the lodash library let _ = require("lodash"); let a = [1, 2, 3, 4, 5, 6]; let newA = _.join(a); console.log("After Join: " + newA); OutputAfter Join: 1,2,3,4,5,6The 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(','). JavaScript const a = [1, 2, 3, 4, 5]; const comma = a.map(String).join(','); console.log(comma); Output1,2,3,4,5 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. Comment More infoAdvertise with us Next Article Create a Comma Separated List from an Array in JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More 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 How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b 3 min read How to Truncate an Array in JavaScript? Here are the different methods to truncate an array in JavaScript1. Using length PropertyIn Array.length property, you can alter the length of the array. It helps you to decide the length up to which you want the array elements to appear in the output.JavaScriptconst n = [1, 2, 3, 4, 5, 6]; n.length 4 min read How to create a string by joining the elements of an array in JavaScript ? Given an array containing array elements and here we will join all the array elements to make a single string. To join the array elements we use arr.join() method. There are two methods by which we can create a string by joining the elements of an array: Table of Content Using arr.join() MethodUsing 2 min read How to convert a DOM nodelist to an array using JavaScript ? NodeList objects are the collection of nodes, usually returned by properties such as Node.childNodes and methods such as document.querySelectorAll(). Although NodeList is not an actual Array but it is possible to iterate over it with the help of forEach() method. NodeList can also be converted into 2 min read Like