JavaScript concat() Method
This tutorial covers the concat() method, which belongs to the JavaScript Array object. The concat() method returns a new array containing the original array element and any arguments passed (any valid array element, including arrays). The concat() method doesn't modify the original array.
This tutorial explains the Array concat() method. Are you looking for the String concat() method?
To add new elements to an existing array (and modify the original array), use the push() method instead.
Using the concat() Method
The script below uses the concat() method to generate a new array with two new elements from an existing array of strings.
var arlene = new Array("alpha", "beta", "gamma");
// Append "delta" and return resulting array
alert( arlene.concat("delta").toString() );
JavaScript's concat() method returns the array resulting from concatenating (putting together) the elements of the original array and the new elements passed as arguments. The original array is not modified.
The toString()
method simply represents the returned array as a string, which the JavaScript interpreter automatically does with alert()
.
Since the JavaScript interpreter works from left to right (and from inside to outside), toString()
applies to the array returned by arlene.concat("delta")
.
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 | |||
---|---|---|---|