Convert Array to String without Commas in JavaScript

avatar

Borislav Hadzhiev

Wed Oct 20 20211 min read

Convert Array to String without Commas #

To convert an array to a string without commas, call the join() method on the array, passing it an empty string as a parameter - arr.join(''). The join method returns a string containing all array elements joined by the provided separator.

index.js
const arr = ['one', 'two', 'three']; const withoutCommas = arr.join(''); console.log(withoutCommas); // ๐Ÿ‘‰๏ธ 'onetwothree' console.log(typeof withoutCommas); // ๐Ÿ‘‰๏ธ string

The only parameter we passed to the Array.join method is a separator.

The method returns a string containing all array elements joined by the provided separator.

If the separator provided to the Array.join method is an empty string, all array elements are joined without any characters in between them.

If your use case requires to join the array elements, by a different character, pass the character as parameter to the join method.

index.js
const arr = ['one', 'two', 'three']; const withSpaces = arr.join(' '); console.log(withSpaces); // ๐Ÿ‘‰๏ธ 'one two three' const withDashes = arr.join('-'); console.log(withDashes); // ๐Ÿ‘‰๏ธ 'one-two-three' const withCommaAndSpace = arr.join(', '); console.log(withCommaAndSpace); // ๐Ÿ‘‰๏ธ 'one, two, three'

If you call the join method on an empty array, an empty string is returned.

index.js
console.log([].join('')); // ๐Ÿ‘‰๏ธ ''

An important thing to note is that, if the array contains elements that are undefined, null or empty array [], they get converted to an empty string.

index.js
console.log(['a', 'b', null].join('')); // ๐Ÿ‘‰๏ธ 'ab'
For most use cases, this behavior works perfectly fine.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee