JavaScript

JS Strings “+” vs Concat Method

Sometimes, developers need to concatenate multiple strings into a single string in various situations, such as creating strings for use in regular expressions, in browser cookies or local storage, in automated testing or dynamic strings for user notifications or messages and so on. More specifically, combining strings in various ways allows developers to create more dynamic and flexible web applications.

This blog will demonstrate the difference between the “+” operator and the “concat()” method in JavaScript.

JS Strings “+” vs “concat()” Method

The “+” operator and the “concat()” method are both used to join/combine two or more strings in JavaScript. The “+” operator is a shorthand way to concatenate strings, while the “concat()” method is a more explicit way to join the strings.

Syntax

Use the given syntax for the “+” operator to join the strings:

string1 + string2

 
The following syntax is used for the “concat()” method:

string.concat(string1, string2, ... , stringN);

 
Example 1: Join Strings Using the “+” Operator

Create two strings “str1” and “str2”:

var str1 = "Welcome To ";
var str2 = "Linuxhint";

 
Use the “+” operator to combine or add these two strings and store the resulting string into the variable “join”:

var join = str1 + str2;

 
Finally, print the concatenated string on the console:

console.log(join);

 
Output


Using it in a numerical value will give the sum of the numbers:


Example 2: Join Strings Using the “concat()” Method

Call the “concat()” method to join the two strings:

var join = str1.concat(str2);

 
Print the resultant string on the console:

console.log(join);

 
Output


Now, let’s try to join numeric values using the “concat()” method. It will give an error because it joins strings not perform any operation like an arithmetic operation:


If you want to join two numbers, use them as a string:

Primary Difference Between “+” Operator and “concat()” Method

The primary difference between the “(+)” and the “concat()” is given below:
 

(+) Operator

 concat() Method

(+) is a JavaScript Operator. concat() is a JavaScript method.
At least two values are required. At least one string is required.
Concatenate the strings and also used to perform an arithmetic operation on numerical data. Only concatenate the string values.
Used for numeric values and also for strings. Only used for the strings.

 
That’s all about the strings “+” operator and “concat()” method in JavaScript.

Conclusion

The “(+)” operator and the “concat()” method are utilized for concatenating strings in JavaScript. The main difference in both is that the “+” operator also combines or adds the numeric values using arithmetic operations. While the concat() method only applies to the strings. In this blog, we demonstrated the difference between the “+” operator and the “concat()” method in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.