Array.concat() (Method)

Concatenate arrays together.

Availability:

ECMAScript edition - 3
JavaScript - 1.2
JScript - 3.0
Internet Explorer - 4.0
Netscape - 4.0
Netscape Enterprise Server - 3.0
Property/method value type:Array object
JavaScript syntax:-myArray.concat(someValues, ...)
Argument list:someValuesA sequence of values to concatenate onto the array.

The result of this method is a new array consisting of the original array, plus the concatenation.

The values that are passed to the method are added to the end of the array.

If arrays are passed, they are flattened and their individual elements added.

The method returns an array consisting of the original Array plus the concatenated values.

If Array1 contains "AAA", "BBB", "CCC" and Array2 contains "000", "111", "222", then the method call Array1.concat(Array2) will return an array with all the elements in a single collection. The original arrays will be untouched.

Warnings:

Example code:

   // Create two arrays and demonstrate concat() method
   myArray1 = new Array("AAA", "BBB", "CCC");
   myArray2 = new Array("000", "111", "222");
   
   document.write("Array1<BR>")
   displayArrayAsTable(myArray1);
   document.write("Array2<BR>")
   displayArrayAsTable(myArray2);
   document.write("Result returned from Array1.concat(Array2)<BR>")
   displayArrayAsTable(myArray1.concat(myArray2));
   document.write("Result returned from Array1.concat('AAA')<BR>")
   displayArrayAsTable(myArray1.concat("AAA"));
   document.write("Result returned from Array1.concat('AAA').concat('DFG')<BR>")
   displayArrayAsTable(myArray1.concat("AAA").concat("DFG"));
   
   // Display an array in a table
   function displayArrayAsTable(anArray)
   {
      myLength = anArray.document.write("<TABLE BORDER=1>");
      for(myIndex = 0; myIndex < myLength; myIndex++)
      {
         document.write("<TR><TD>");
         document.write(myIndex);
         document.write("</TD><TD>");
         document.write(anArray[myIndex]);
         document.write("</TD></TR>");
      }
      document.write("</TABLE><BR><BR>")
   }

See also:Array.prototype, String.concat()

insert figure 0102

Cross-references:

ECMA 262 edition 3 - section - 15.4.4.4