Last updated: Mar 3, 2024
Reading timeยท3 min
To concatenate strings with a separator:
Array.join()
method.join()
method will concatenate the strings with the specified
separator.const str1 = 'bobby'; const str2 = 'hadz'; const str3 = 'com'; const commaSeparated = [str1, str2, str3].join(','); console.log(commaSeparated); // ๐๏ธ "bobby,hadz,com" const hyphenSeparated = [str1, str2, str3].join('-'); console.log(hyphenSeparated); // ๐๏ธ "bobby-hadz-com"
You can specify any string as the separator, e.g. an underscore, a word, a space, etc.
The Array.join() method concatenates all of the elements in an array using a separator.
Array.join()
method takes is a separator
- the string used to separate the elements of the array.If some of the strings are empty, you will get multiple separators next to one another.
const str1 = 'bobby'; const str2 = 'hadz'; const str3 = ''; const str4 = 'com'; const result = [str1, str2, str3, str4].join('#'); console.log(result); // ๐๏ธ bobby#hadz##com
Notice that there are two hash symbols next to one another because the str3
variable stores an empty string.
You can use the Array.filter()
method to remove the empty strings from the
array before calling join()
.
const str1 = 'bobby'; const str2 = 'hadz'; const str3 = ''; const str4 = 'com'; const result = [str1, str2, str3, str4].filter(element => element).join('#'); console.log(result); // ๐๏ธ bobby#hadz#com
The function we passed to the filter()
method gets called with each element in
the array.
On each iteration, we return the current element to exclude all falsy values (e.g. empty strings) from the new array.
The filter()
method returns a new array that only contains the elements that
meet the condition.
If a value for the separator
argument is omitted, the array elements are
joined with a comma ,
.
const str1 = 'bobby'; const str2 = 'hadz'; const str3 = 'com'; const withoutSeparator = [str1, str2, str3].join(); console.log(withoutSeparator); // ๐๏ธ "bobby,hadz,com"
Even if that's what you need, it's a best practice to be explicit and pass the
comma to the join()
method, as not all developers are aware of the default
behavior.
If the separator
argument is set to an empty string, the array elements are
joined without any characters in between them.
const str1 = 'bobby'; const str2 = 'hadz'; const str3 = 'com'; const withoutSeparator = [str1, str2, str3].join(''); console.log(withoutSeparator); // ๐๏ธ "bobbyhadzcom"
If the join
method is called on an empty array, an empty string is returned.
console.log([].join(',')); // ๐๏ธ ""
Note that any undefined
, null
, or empty array []
values get converted to
an empty string.
// ๐๏ธ "bobby,hadz," console.log(['bobby', 'hadz', undefined].join(','));
If you see a trailing separator in the result, chances are your array contains
an undefined
, null
or []
value.
As shown in the previous example, you can use the Array.filter()
method to
remove all falsy values from the array before calling join()
.
const str0 = ''; const str1 = 'bobby'; const str2 = undefined; const str3 = 'hadz'; const str4 = ''; const result = [str0, str1, str2, str3, str4] .filter(element => element) .join('-'); console.log(result); // ๐๏ธ bobby-hadz
We used the filter()
method to exclude all falsy values from the array before
calling join()
.
The falsy values in JavaScript are: false
, 0
, -0
, ""
(empty string),
null
, undefined
, NaN
(Not a number).
You can learn more about the related topics by checking out the following tutorials: