Convert Array to String without Commas in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

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 Array.join method is called with an empty string separator, all array elements are joined without any characters in between.

If your use case requires you to join the array elements with 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('')); // 👉️ ''

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 #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.