How to create a comma separated string from a list of string in Javascript?
This example joins the element of the array by join() method using comma(, ).
Example 1
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript
| Create comma separated list from an array.
</title>
</head>
<body style="text-align:center;" id="body">
<h1 style="color:green;">
GeeksForGeeks
</h1>
<p id="GFG_UP" style="font-size: 16px;
font-weight: bold;">
</p>
<button onclick="gfg_Run()">
click here
</button>
<p id="GFG_DOWN" style="color:green;
font-size: 20px;
font-weight: bold;">
</p>
<script>
var el_up = document.getElementById("GFG_UP");
var el_down = document.getElementById("GFG_DOWN");
var arr = ["GFG1", "GFG2", "GFG3"];
el_up.innerHTML = 'Original Array = ["' + arr[0] + '", '
+ arr[1] + '", '
+ arr[2] + '"'
+ ']';;
function gfg_Run() {
el_down.innerHTML =
"Comma separates list = " + arr.join(", ");
}
</script>
</body>
</html>
Output

Example 2
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript
| Create comma separated list from an array.
</title>
</head>
<body style="text-align:center;" id="body">
<h1 style="color:green;">
GeeksForGeeks
</h1>
<p id="GFG_UP"
style="font-size: 16px;
font-weight: bold;">
</p>
<button onclick="gfg_Run()">
click here
</button>
<p id="GFG_DOWN" style="color:green;
font-size: 20px;
font-weight: bold;">
</p>
<script>
var el_up = document.getElementById("GFG_UP");
var el_down = document.getElementById("GFG_DOWN");
var arr = ["GFG1", "GFG2", "GFG3"];
el_up.innerHTML = 'Original Array = ["' + arr[0] + '", '
+ arr[1] + '", '
+ arr[2] + '"'
+ ']';;
function gfg_Run() {
el_down.innerHTML =
"Comma separates list = " + arr.toString();
}
</script>
</body>
</html>
Output
