JavaScript concat() Method String.concat() Method

This tutorial introduces the concat() method, from the JavaScript String object. The concat() method returns a new string from the string on which it is applied, and the string passed as argument. The concat() method does not modify the original string, and only a copy of the result is returned.

This tutorial explains the String concat() method. Are you looking for the Array concat() method?

Using the concat() Method with One Argument

The concat() method accepts either a declared string as variable, or an anonymous string (a fragment of quoted text). Likewise, the concat() method can be applied on either on a declared string variable (as shown below), or on an anonymous string (see two scripts down).

var carter = new String("I learn JavaScript...");
alert( carter.concat(" to make better websites.") );

Our script declared a string variable, and used the concat() method to generate a new string by appending the anonymous string passed as argument. Note that our original string remains untouched. JavaScript concat() method

We created our new string by applying the concat() method on a declared variable, and passing an anonymous string as argument. The exact same result can be achieved by doing the reverse:

var carter = new String(" to make better websites.");
alert( "I learn JavaScript...".concat(carter) );

Don't forget to use a connecting period (.) to join your quoted strings with the concat() method.

The web browser's JavaScript interpreter might otherwise throw a nasty error message at your users.

The String Concatenation Operator in JavaScript

In some cases, using JavaScript's built-in string concatenation operator is simpler to use than the concat() method. The concatenation operator is simply the plus sign, used between quoted strings or string variables.

var carter = "I learn JavaScript...";
alert( carter + " and CSS" );
// Same as: carter.concat(" and CSS");

The last line of code returns the following string: JavaScript string concatenation operator

Using the concat() Method with Multiple Arguments

The concat() method takes at least one argument, but accepts multiple string arguments, separated by commas. The script below declares two strings, and uses the concat() method to concatenate (join) the initial strings with a third one, passed as argument.

var carter1 = new String("I learn JavaScript...");
var carter2 = "having fun.";
alert( carter1.concat( " and I am ", carter2 ) );

This last JavaScript code uses both a named string variable and an anonymous string, and concatenates them to a declared string variable: JavaScript concat() method with 2 arguments

We limited ourselves to two arguments, but you can (theoretically) pass as many strings as you want to the concat() method.

Test the concat() Method

Interactively test the concat() method by editing the JavaScript code below and clicking the Test concat() Method button.

Browser support for JavaScript concat() method
Internet Explorer supports the JavaScript concat() methodFirefox supports the JavaScript concat() methodSafari supports the JavaScript concat() methodOpera supports the JavaScript concat() method