Array.join() method to convert an array into a human-readable string. This method takes an array as input and returns the array as a string.
By default, the elements are joined together by using a comma (
,) as a separator:
const fruits = ['Apple', 'Orange', 'Mango', 'Cherry']; const str = fruits.join(); console.log(str); // Apple,Orange,Mango,Cherry
You can also specify a custom separator — dashes, spaces, or empty strings — as a parameter to the
Array.join() method. Let us say that you want to add a space after the comma, just pass the following to
const str = fruits.join(', '); // Apple, Orange, Mango, Cherry
Want to have each array element on its own line? Just pass in a newline character:
const str = fruits.join('\n'); // Apple // Orange // Mango // Cherry
Prefer to use the HTML line break?
const str = fruits.join('<br>');
Array.join() method works in all modern browsers, and back to at least IE6.