Concatenation is a technique used in programming to merge two or more strings into one value. For example, you may have two lists of employees stored as strings, one for each branch in your company. You may want to merge them all into one list of all your employees.
There are two ways to concatenate a string:
- Using the concatenation operator (+).
- Using the concat() method.
The concat() method has browser support on all modern browsers.
There is a method called Array.concat() that concatenates two arrays. In this article we’re going to focus on the string concat() method.
Here is the syntax for the concatenation operator:
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
"string1" + "string2"
Concatenation is useful if you have separate string values that you want to appear as one. For instance, say that you have two strings. One string contains the forename of a user. The other string contains the surname. You can merge these two values to get the whole name as one string.
Say we want to merge two strings together. One string is the forename of a customer. Another string is the surname of that customer. Let’s merge these two strings using the “+” operator:
var bill_johnson = "Bill" + "Johnson"; console.log(bill_johnson);
Our code returns: “BillJohnson”. Our strings are not automatically separated by a space. If we want a space between our strings, we need to specify one within one of our strings:
"Bill " + "Johnson"
Our code now returns: “Bill Johnson”. This is because we added a space in the “Bill ” string.
string_name.concat(string_one, string_two, string_three);
You can pass as many parameters as you would like into concat().
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
Let’s use an example to illustrate how the concat() string function works in action. Let’s say that we have a user’s first name, middle and second name and want to combine them into one string. We could use the following code to achieve this goal:
var first_name = 'Lucy'; var middle_name = ' Turnbull'; var second_name = ' Cooper'; console.log(name_string.concat(middle_name, surname));
Our code returns: “Lucy Turnbull Cooper”. It’s important to note that we included spaces at the start of the “middle_name” and “second_name” strings. This ensures that our end string was separated by spaces.
In addition, you can also use the concat() function to combine more than two strings. Here’s an example of concat() being used to combine three strings together:
var good_morning = 'Good'; console.log(good_morning.concat(' Morning', 'Alex!', ' It is a great day!'));
Our code returns a new string: “Good Morning, Alex! It is a great day!”
var good_morning = 'Good'; var new_string = good_morning.concat(' Morning', 'Alex!', ' It is a great day!') console.log(new_string);
Our code returns the same as above, but now our new string is stored within the variable “new_string.”
Both of these methods can be used interchangeably. This means that neither one offers any special methods. Both the plus sign and the concat() method merge strings to create a new string.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.