concat() in JavaScript
Overview
The concat() string method in javascript combines two or more strings and returns a new string. This method doesn't make any change in the original string. This concat() method is a function of the String object in javascript therefore, it has to be invoked through a particular instance of the String class.
What is String concat() in JavaScript?
The concat() method in javascript concatenates the string arguments to the calling string and returns a new string. Changes to the original string or the returned string don't affect the other.
If the arguments are not of the type string, then they have to be converted to string values before combining.
Performance of JavaScript String concat()
The official statement from MDN, regarding the performance benefits of concatenating strings using the concat() method or the operators is:
It is strongly recommended that the assignment operators (+, +=) are used instead of the concat() method.
Both the + operator and concat() method in javascript are pretty the same but using + provides more readability and also + operator is faster than concat() for performance.
However, the below program demonstrates how the concat() can outperform the + operator:
Output
Syntax of String concat() in JavaScript
The syntax of the String method concat in javascript is:
Parameter of String concat() in JavaScript
str_1, str_2, ..., str_n - represents the strings values to be combined.
Return Value of String concat() in JavaScript
The concat() method in Javascript returns a new string as a result of concatenating the strings passed in the function as parameters.
Exceptions of String concat() in Javascript
Output
So while using concat in javascript, we have to make sure that each of the parameter values will be converted to a string, if necessary, before the concatenation is performed.
Example
Let us understand the working of concat in javascript with the help of following example
Output
In the above program, we stored 'Scaler' in str1 and Learners in str2. Using the str1.concat(' ',str2), we combinedstr1 and str2 and returned the combined string which is Scaler Academy as the result.
More Examples
-
Combining strings into a new string
Output
In the above example, we have declared a variable named greeting and is assigned the string value 'Good, '. We have then implemented the concat() method on the greeting variable to append the two string values to the end of the value of the greeting. The value of the original greeting string variable has not changed and is equal to 'Good, '
-
Using an Empty String Variable
We can use the concat() in javascript method with an empty string variable to concatenate primitive string values.
Output
In the above program, we declared a variable called str1 as an empty string or '', and then we have invoked the String concat() in javascript to concatenate primitive string values together.
Conclusion
- The concat() method in Javascript combines two or more strings and returns a new string while the value of the original string remains unchanged.
- If required, every parameter value passed into the concat in javascript has to be converted to a string, before the concatenation operation is performed.
- It is strongly recommended to use the assignment operators (+, +=) instead of the concat in javascript.