JavaScript – Concatenate Strings

To concatenate string(s) to this string in JavaScript, call concat() method on this string, and pass all the other strings to be concatenated to this string, as arguments.

concat() method does not modify the original strings, and returns a new string formed by concatenating the given strings.

Syntax

The syntax to concatenate strings: str1 and str2 is

str1.concat(str2)

The syntax to concatenate strings: str1, str2, and str3 is

str1.concat(str2, str3)

Similarly, we can concatenate multiple strings using concat() method.

str1.concat(str2, str3, str4, ..., strN)
ADVERTISEMENT

Examples

The following is a quick example to concatenate two strings in JavaScript.

var str1 = 'apple ';
var str2 = 'banana';
var result = str1.concat(str2);

In the following example, we take two strings: str1 and str2 in script, and concatenate these two strings using concat() method. We shall display the resulting string in a div.

index.html

Now, let us concatenate more than two strings.

The following is a quick example to concatenate four strings in JavaScript.

var str1 = 'apple ';
var str2 = 'banana ';
var str3 = 'guava ';
var str4 = 'cherry';
var result = str1.concat(str2, str3, str4);

In the following example, we take four strings: str1, str2, str3, and str4 in script, and concatenate these four strings in the specified order using concat() method. We shall display the resulting string in a div.

index.html

Conclusion

In this JavaScript Tutorial, we learned how to concatenate strings in JavaScript, using concat() method, with examples.