How to Convert Array to Comma Separated String in JavaScript?

22-Apr-2023

.

Admin

How to Convert Array to Comma Separated String in JavaScript?

Today, I will let you know example of how to convert array to comma separated string in javascript. this example will help you convert comma separated string to array using javascript. We will look at example of how to convert array into comma separated string in javascript. if you have question about convert array to comma separated string javascript then I will give simple example with solution.

There are different ways to convert an array to a comma-separated string in JavaScript. Here are some examples:

1. Using the join() method: The join() method joins all elements of an array into a string, separated by a specified separator (in this case, a comma).

Example 1:


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Convert Array to Comma Separated String in JavaScript? - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

const arr = ["apple", "banana", "orange"];

const str = arr.join(","); // "apple,banana,orange"

</script>

</html>

2. Using the toString() method: The toString() method converts an array to a string and separates the elements with commas.

Example 2:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Convert Array to Comma Separated String in JavaScript? - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

const arr = ["apple", "banana", "orange"];

const str = arr.toString(); // "apple,banana,orange"

</script>

</html>

3. Using a loop: You can also use a loop to iterate over the elements of the array and concatenate them into a string with commas.

Example 3:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Convert Array to Comma Separated String in JavaScript? - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

const arr = ["apple", "banana", "orange"];

let str = "";

for (let i = 0; i < arr.length; i++) {

if (i > 0) {

str += ",";

}

str += arr[i];

}

// "apple,banana,orange"

</script>

</html>

4. Using reduce(): The reduce() method applies a function against an accumulator and each element in the array to reduce it to a single value. In this case, we're using it to concatenate the elements with commas.

Example 4:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Convert Array to Comma Separated String in JavaScript? - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

const arr = ["apple", "banana", "orange"];

const str = arr.reduce((acc, val) => acc + "," + val); // "apple,banana,orange"

</script>

</html>

#JavaScript