How to convert javascript array to CSV?

It is very simple to convert an array to CSV. Just have a look.

Suppose you have a JS array of strings like this:

var vegetables= [‘onion’, ‘ladyfinger’, ‘peas’];
var str = vegetables.valueOf();

The valueOf() method will convert an array in javascript to a comma separated string.

Output:

onion, ladyfinger, peas