Categories
arrays javascript

Add different delimiters in javascript toString()..?

normally JavaScript’s toString() method returns the array in a comma seperated value like this

 var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
var result = myArray .toString();

And it returns the output like this zero,one,two,three,four,five.

But I have a requirement to represent the result in this format zero_one_two_three_four_five (replacing the comma with _).

I know we can do this using replace method after converting the array to string. Is there a better alternative available?

Cheers

Ramesh Vel

myArray.join(‘_’) should do what you need.