concat() in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.

See Also

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more