Combine multiple strings with a delimiter in JavaScript
This post will discuss how to combine multiple strings with a delimiter in JavaScript.
There are several ways to combine multiple strings with a delimiter in JavaScript. Here are some of the most common functions, along with some examples and explanations:
1. Using join() function
One way is to use the join() function, which is a built-in function of the array object. This function takes one argument, the delimiter string to be used as the separator between the array elements. It returns a new string that is the concatenation of the array elements with the delimiter. We can use this function to join multiple strings by first putting them into an array and then passing the desired delimiter as the separator. For example, to join three strings with a comma:
|
1 2 3 4 5 6 7 |
// The array of strings to join const arr = ["Red", "Green", "Blue"]; // Use the join function to join the array elements with a comma const joinedString = arr.join(","); console.log(joinedString); // "Red,Green,Blue" |
2. Using + operator
Another way is to use the + operator, which is a arithmetic operator that can also be used for string concatenation. This operator takes two strings and returns a new string that is the result of appending the second string to the first one. We can use this operator to concatenate multiple strings with a delimiter string in between. For example, if we have three strings, we can join them using a comma and a space as the delimiter like this:
|
1 2 3 4 5 6 7 8 |
// The strings to join const str1 = "Red"; const str2 = "Green"; const str3 = "Blue"; const joinedString = str1 + "," + str2 + "," + str3; console.log(joinedString); // "Red,Green,Blue" |
3. Using concat() function
A third way is to use the concat() function, which is a built-in function of the string object. This function takes one or more strings as arguments and returns a new string that is the concatenation of the original string and the arguments. We can use this function to join multiple strings by passing them as arguments to the concat() function. However, we need to manually add the delimiter between each string. For example, to join three strings with a comma:
|
1 2 3 4 5 6 7 8 9 |
// The strings to join const str1 = "Red"; const str2 = "Green"; const str3 = "Blue"; // Use the concat function to join the strings with a comma let joinedString = str1.concat(",", str2, ",", str3); console.log(joinedString); // "Red,Green,Blue" |
4. Using template literals
The template literals are a newer feature of JavaScript that allow us to create strings with embedded expressions and variables. We can use it to join multiple strings by enclosing them in backticks (` `) and using the ${} syntax to insert placeholders for strings that are evaluated and replaced by their values. We can also add the delimiter between each expression. For example, to join three strings with a comma:
|
1 2 3 4 5 6 7 8 9 |
// The strings to join const str1 = "Red"; const str2 = "Green"; const str3 = "Blue"; // Use template literals to join the strings with a comma let joinedString = `${str1},${str2},${str3}`; console.log(joinedString); // "Red,Green,Blue" |
That’s all about combining multiple strings 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 :)