Let’s say we have following array and we wan’t to convert the
cars array to string.
const cars = ['Audi', 'BMW', 'MERC']
First option is to use
toString() on array. The
toString() method will join the array items separated by comma and return a string representation of array.
Another, way to convert array to string is to use the
join() method works similar to
toString() method however you can also specify separator while calling the method.
If you call the
join() method on the array without specifying any separator it returns the same output as
Tip: If the array has only one item, then that item will be returned without using the separator.
Tip: The above method works well for nested arrays containing numbers and strings however, if you have an array of objects you might not get expected results and get output in [object Object] form.