Array.prototype.concat()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/concat
  • Revision title: Array.prototype.concat
  • Revision id: 428339
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

Summary

Returns a new array comprised of this array joined with other array(s) and/or value(s).

Method of Array instances
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 3rd Edition

Syntax

array.concat(value1, value2, ..., valueN)

Parameters

valueN
Arrays and/or values to concatenate to the resulting array.

Description

concat creates a new array consisting of the elements in the this object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array).

concat does not alter this or any of the arrays provided as arguments but instead returns a shallow 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. That is, if a referenced object is modified, the changes are visible to both the new and original arrays.
  • Strings and numbers (not String and Number objects): concat copies the values of strings and numbers into the new array.

Any operation on the new array will have no effect on the original arrays, and vice versa.

Examples

Example: Concatenating two arrays

The following code concatenates two arrays:

var alpha = ["a", "b", "c"];
var numeric = [1, 2, 3];

// creates array ["a", "b", "c", 1, 2, 3]; alpha and numeric are unchanged
var alphaNumeric = alpha.concat(numeric);

Example: Concatenating three arrays

The following code concatenates three arrays:

var num1 = [1, 2, 3];
var num2 = [4, 5, 6];
var num3 = [7, 8, 9];

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

Example: Concatenating values to an array

The following code concatenates three values to an array:

var alpha = ['a', 'b', 'c'];

// creates array ["a", "b", "c", 1, 2, 3], leaving alpha unchanged
var alphaNumeric = alpha.concat(1, [2, 3]);

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a new array comprised of this array joined with other array(s) and/or value(s).</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Array_instances"><code>Array</code> instances</a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.2</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 3rd Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>array</em>.concat(<em>value1</em>, <em>value2</em>, ..., <em>valueN</em>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>value<em>N</em></code></dt>
  <dd>
    Arrays and/or values to concatenate to the resulting array.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p><code>concat</code> creates a new array consisting of the elements in the <code>this</code> object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array).</p>
<p><code>concat</code> does not alter <code>this</code> or any of the arrays provided as arguments but instead returns a shallow 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. That is, if a referenced object is modified, the changes are visible to both the new and original arrays.</li>
</ul>
<ul>
  <li>Strings and numbers (not <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">String</a> and <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a> objects): <code>concat</code> copies the values of strings and numbers into the new array.</li>
</ul>
<p>Any operation on the new array will have no effect on the original arrays, and vice versa.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Concatenating_two_arrays" name="Example:_Concatenating_two_arrays">Example: Concatenating two arrays</h3>
<p>The following code concatenates two arrays:</p>
<pre class="brush: js">
var alpha = ["a", "b", "c"];
var numeric = [1, 2, 3];

// creates array ["a", "b", "c", 1, 2, 3]; alpha and numeric are unchanged
var alphaNumeric = alpha.concat(numeric);
</pre>
<h3 id="Example:_Concatenating_three_arrays" name="Example:_Concatenating_three_arrays">Example: Concatenating three arrays</h3>
<p>The following code concatenates three arrays:</p>
<pre class="brush: js">
var num1 = [1, 2, 3];
var num2 = [4, 5, 6];
var num3 = [7, 8, 9];

// creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged
var nums = num1.concat(num2, num3);
</pre>
<h3 id="Example:_Concatenating_values_to_an_array" name="Example:_Concatenating_values_to_an_array">Example: Concatenating values to an array</h3>
<p>The following code concatenates three values to an array:</p>
<pre class="brush: js">
var alpha = ['a', 'b', 'c'];

// creates array ["a", "b", "c", 1, 2, 3], leaving alpha unchanged
var alphaNumeric = alpha.concat(1, [2, 3]);
</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push"><code>push</code></a> / <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop"><code>pop</code></a> - add/remove elements from the end of the array;</li>
  <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift"><code>shift</code></a> / <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift"><code>unshift</code></a> - add/remove elements from the beginning of the array;</li>
  <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice"><code>splice</code></a> - add/remove elements from the specified location of the array.</li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat"><code>String.prototype.concat</code></a></li>
</ul>
<!--languages({
"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/concat"
-->
Revert to this revision