Join array elements together with a delimiter in JavaScript
This post will discuss how to join elements of an array with a delimiter in JavaScript.
1. Using Array.join() function
One way to join elements of an array using delimiter in JavaScript is to use the Array.join() function. This function joins the elements of an array into a string, separated by a specified delimiter. For example, if we have an array of numbers and we want to convert it to a string using a dash (-) as a separator, we can do this:
|
1 2 3 4 5 |
let array = ["a", "b", "c", "d", "e"]; let string = array.join("-"); console.log(string); // "a-b-c-d-e" |
We can use any character or string as the delimiter, such as a comma (,), a space ( ), or a plus sign (+). If we don’t provide any argument to the join() function, it will use the default delimiter, which is a comma (,). Here’s an example:
|
1 2 3 4 5 |
let array = ["a", "b", "c", "d", "e"]; let string = array.join(); console.log(string); // "a,b,c,d,e" |
2. Using Array.reduce() function
Another way to convert an array to a string using a delimiter is to use the Array.reduce() function. This is a third built-in function that executes a reducer function on each element of the array, resulting in a single output value. We can use this function to convert an array to a string by concatenating each element with a delimiter of our choice. For example, if we have an array of strings and we want to convert it to a string using a slash (/) as the delimiter, we can do this:
|
1 2 3 4 5 |
let array = ["January", "February", "March"]; let q1 = array.reduce((acc, curr) => acc + "/" + curr); console.log(q1); // "January/February/March" |
We can also provide an initial value for the accumulator as the second argument to the reduce() function. Here’s an example:
|
1 2 3 4 5 |
let array = ["February", "March"]; let q1 = array.reduce((acc, curr) => acc + "/" + curr, "January"); console.log(q1); // "January/February/March" |
3. Using Array.toString() function
This is another built-in function that returns a string representing the specified array and its elements. This function is equivalent to calling the join() function without any arguments, which means it uses a comma as the default separator. For example:
|
1 2 3 4 5 |
let array = ["a", "b", "c", "d", "e"]; let string = array.toString(); console.log(string); // "a,b,c,d,e" |
4. Using Array.map() function
We can also use other functions, such as Array.map(), to join the array elements with a delimiter and perform some additional operations on them. For example, if we want to join the array elements with a space ( ) and capitalize the first letter of each element, we can use the Array.map() function like this:
|
1 2 3 4 5 |
let array = ["a", "b", "c", "d", "e"]; let string = array.map(cur => cur[0].toUpperCase() + cur.slice(1)).join(", "); console.log(string); // "A, B, C, D, E" |
That’s all about joining elements of an array with a delimiter in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)