Convert array to comma Separated String Javascript?

In this tutorial explained to convert array to comma separated string javascript. how we can convert array to string with commas or without commas.

I find out way to convert object array to comma separated string two way in javascript. using toString() and join() function of array property. we have exaplain examples for your help. 

Let’s see both example and you can use examples.

Example 1:
convert array to string using toString() function
<script type="text/javascript">

var dataval = ["first","second","third"];

document.write(dataval.toString());

</script>
Output:
first,second,third
Example 2:
<script type="text/javascript">

var dataval = ["first","second","third"];

document.write(dataval.join('='));

</script>
Output:
first=second=third
Explanation of convert array to comma separated string javascript
  1. we have an array called dataval containing various datavalue.
  2. we use the toString() and join() function.This method join all elements of  the array into single string, separated by the specified separator.
  3. the separator we specify is ‘=’,which means each element in the resulting string will be separated by a comma followed a space.

I hope this tutorial help you..

Leave a Reply

Your email address will not be published. Required fields are marked *