Combine Two Strings with Concat() in Node.js
How do you use concat()
method to combine two strings in Node.js?
The concat()
method concatenates a string argument to the calling string and returns a new string.
In the examples below, we will take two strings and combine them using the concat()
method.
Here's the full code:
const str1 = "Taco"
const str2 = "Bell"
const newString = str1.concat(str2)
// newString = "TacoBell"
And we can add a space between the two words with either of the two methods below:
const newString = str1.concat(" ").concat(str2)
// newString = "Taco Bell"
const newString = str1.concat(" ", str2)
// newString = "Taco Bell"
As you may have noticed, the concat()
method takes a variable number of arguments. And each argument will be concatenated to the original string.
For more information on the concat()
method, check out the Mozilla Web Docs page on the subject.