Last updated: Mar 2, 2024
Reading timeยท4 min
You can use the String()
constructor to convert an array to a comma-separated
string.
The String()
constructor will return a string where the array elements are
separated by commas.
const arr = ['bobby', 'hadz', 'com']; // โ using String() constructor const str = String(arr); console.log(str); // ๐๏ธ 'bobby,hadz,com' console.log(typeof str); // ๐๏ธ string // ------------------------------------------- // โ Using toString() method const str2 = arr.toString(); console.log(str2); // ๐๏ธ 'bobby,hadz,com' console.log(typeof str2); // ๐๏ธ string
The only argument we passed to the String object is the value we want to convert to a string.
Since we provided an array, the result is a comma-separated string.
The String()
constructor and the
Array.toString()
methods use the join()
method to convert the array into a comma-separated
string under the hood.
Array.join()
An alternative, but also very common approach is to use the Array.join() method.
The Array.join()
method will return a string where all of the array elements
are joined with a comma separator.
// โ Convert an array to a comma-separated string const arr = ['bobby', 'hadz', 'com']; const str = arr.join(','); console.log(str); // ๐๏ธ 'one,two,three' const str2 = arr.join(', '); console.log(str2); // ๐๏ธ "bobby, hadz, com"
To convert an array to a string without commas, pass an empty string to the
Array.join()
method.
const arr = ['bobby', 'hadz', '.com']; const withoutCommas = arr.join(''); console.log(withoutCommas); // ๐๏ธ 'bobbyhadz.com' console.log(typeof withoutCommas); // ๐๏ธ string
If the separator
argument is set to an empty string, the array elements are
joined without any characters in between them.
The Array.join() method concatenates all of the elements in an array using a separator.
The only argument the Array.join()
method takes is a separator
- the string
used to separate the elements of the array.
If a value for the separator
argument is omitted, the array elements are
joined with a comma ,
.
You can pass any value for the separator to the Array.join()
method. Here are
some other examples.
const arr = ['bobby', 'hadz', 'com']; const withSpaces = arr.join(' '); console.log(withSpaces); // ๐๏ธ 'bobby hadz com' const withCommaAndSpace = arr.join(', '); console.log(withCommaAndSpace); // ๐๏ธ bobby, hadz, com const withDashes = arr.join('-'); console.log(withDashes); // ๐๏ธ 'bobby-hadz-com' const withoutSeparator = arr.join(''); console.log(withoutSeparator); // ๐๏ธ bobbyhadzcom
If you call the join
method on an empty array, it returns an empty string.
console.log([].join(',')); // ๐๏ธ ''
undefined
and null
valuesAn important thing to note is that if the array contains elements that are
undefined
, null
or an empty array []
, they get converted to an empty
string.
const arr = ['bobby', 'hadz', null]; console.log(String(arr)); // ๐๏ธ bobby,hadz, console.log(arr.join(',')); // ๐๏ธ bobby,hadz,
If you need to
remove the null
and undefined
values from an array
before calling the Array.join()
method use the Array.filter()
method.
const arr = [null, 'bobby', 'hadz', null, undefined, 'com']; const str = arr .filter(element => { return element !== null && element !== undefined; }) .join(','); console.log(str); // ๐๏ธ bobby,hadz,com
The function we passed to the Array.filter()
method gets called with each
element in the array.
On each iteration, we check if the current element is not equal to null and undefined and return the result.
const arr = [null, 'bobby', 'hadz', null, undefined, 'com']; // ๐๏ธ [ 'bobby', 'hadz', 'com' ] console.log( arr.filter(element => { return element !== null && element !== undefined; }), );
The filter()
method returns a new array that only contains the elements that
meet the condition.
The last step is to use the Array.join()
method to convert the array to a
comma-separated string.
If you have to do this often, define a reusable function.
// โ convert an array to a comma-separated string function arrayToCommaString(array) { return array .filter(element => { return element !== null && element !== undefined; }) .join(','); } const arr = [null, 'bobby', 'hadz', null, undefined, 'com']; const str = arrayToCommaString(arr); console.log(str); // ๐๏ธ bobby,hadz,com
The function takes an array as a parameter and converts the array to a comma-separated string.
You can call the join()
method with an empty string to convert the array to a
string without commas.
function toStringWithoutCommas(array, separator = '') { return array .filter(element => { return element !== null && element !== undefined; }) .join(separator); } const arr = [null, 'bobby', undefined, 'hadz', null, 'com']; const str1 = toStringWithoutCommas(arr); console.log(str1); // ๐๏ธ "bobbyhadzcom" const str2 = toStringWithoutCommas(arr, '-'); console.log(str2); // ๐๏ธ "bobby-hadz-com"
The function takes an array and optionally a separator and converts the array to a string without commas.
To convert an array to a string without commas:
Array.map()
method to iterate over the array.replaceAll()
method to remove all commas from each array element.Array.join()
method to join the array to a string without commas.const arr = ['bo,bby', 'ha,dz', 'c,om']; const str = arr .map(element => { return element.replaceAll(',', ''); }) .join(''); console.log(str); // ๐๏ธ bobbyhadzcom
The function we passed to the Array.map() method gets called with each element in the array.
On each iteration, we use the String.replaceAll()
method to remove all commas
from the string and return the result.
The map()
method returns a new array containing the values returned from the
callback function.
const arr = ['bo,bby', 'ha,dz', 'c,om']; // ๐๏ธ [ 'bobby', 'hadz', 'com' ] console.log( arr.map(element => { return element.replaceAll(',', ''); }), );
The String.replaceAll() method returns a new string with all matches of a pattern replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
The String.replaceAll()
method returns a new string with the matches of the
pattern replaced. The method doesn't change the original string.
The last step is to use the Array.join()
method to join the array into a
string without commas.
const arr = ['bo,bby', 'ha,dz', 'c,om']; const str = arr .map(element => { return element.replaceAll(',', ''); }) .join(''); console.log(str); // ๐๏ธ bobbyhadzcom
We joined the array elements without a separator, but you can use any other value.
You can learn more about the related topics by checking out the following tutorials: