Array.prototype.concat()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/concat
  • Revision title: Array concat method
  • Revision id: 26311
  • Created:
  • Creator: Maian
  • Is current revision? No
  • Comment Core JavaScript 1.5 Reference:Objects:Array:concat moved to Core JavaScript 1.5 Reference:Global Objects:Array:concat

Revision Content

Summary

Joins two arrays and returns a new array.

Method of Array
Implemented in: JavaScript 1.2, NES 3.0
ECMA Version: ECMA-262

Syntax

concat(arrayName2, arrayName3, ..., arrayNameN)

Parameters

arrayName2...arrayNameN 
Arrays to concatenate to this array.

Description

concat does not alter the original arrays, but returns a "one level deep" copy that contains copies of the same elements combined from the original arrays. Elements of the original arrays are copied into the new array as follows:

  • Object references (and not the actual object): concat copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
  • Strings and numbers (not String and Number objects): concat copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other arrays.

If a new element is added to either array, the other array is not affected.

Examples

Example: Concatenating two arrays

The following code concatenates two arrays:

alpha=new Array("a","b","c")
numeric=new Array(1,2,3)
alphaNumeric=alpha.concat(numeric) // creates array ["a","b","c",1,2,3]

Example: Concatenating three arrays

The following code concatenates three arrays:

num1=[1,2,3]
num2=[4,5,6]
num3=[7,8,9]
nums=num1.concat(num2,num3) // creates array [1,2,3,4,5,6,7,8,9]

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Joins two arrays and returns a new array.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.2, NES 3.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
concat(<i>arrayName2</i>, <i>arrayName3</i>, ..., <i>arrayNameN</i>)
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>arrayName2...arrayName<i>N</i></code> </dt><dd> Arrays to concatenate to this array.
</dd></dl>
<h3 name="Description"> Description </h3>
<p><code>concat</code> does not alter the original arrays, but returns a "one level deep" copy that contains copies of the same elements combined from the original arrays. Elements of the original arrays are copied into the new array as follows:
</p>
<ul><li> Object references (and not the actual object): <code>concat</code> copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
</li></ul>
<ul><li> Strings and numbers (not <a href="en/Core_JavaScript_1.5_Reference/Objects/String">String</a> and <a href="en/Core_JavaScript_1.5_Reference/Objects/Number">Number</a> objects): <code>concat</code> copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other arrays.
</li></ul>
<p>If a new element is added to either array, the other array is not affected.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Concatenating_two_arrays"> Example: Concatenating two arrays </h4>
<p>The following code concatenates two arrays:
</p>
<pre>alpha=new Array("a","b","c")
numeric=new Array(1,2,3)
alphaNumeric=alpha.concat(numeric) // creates array ["a","b","c",1,2,3]
</pre>
<h4 name="Example:_Concatenating_three_arrays"> Example: Concatenating three arrays </h4>
<p>The following code concatenates three arrays:
</p>
<pre>num1=[1,2,3]
num2=[4,5,6]
num3=[7,8,9]
nums=num1.concat(num2,num3) // creates array [1,2,3,4,5,6,7,8,9]
</pre>
Revert to this revision