Archive for May 13, 2007

JavaScript Array concat Method

Posted in JavaScript, Software, Web, internet, programming on May 13, 2007 by Joey

The concat method of the JavaScript Array object is used to create a new array from an array, while appending any arguments passed to the concat method to the array on which the concat method was called.

var aryTest = [1,2];
var aryNew = aryTest.concat(3);
document.write(aryNew);

The preceding example appends creates a new array from an existing array and appends the value 3 to it.

If an array is passed as one of the parameters to the concat method, that array is converted to a string and then added to the array on which that concat method was called. This is demonstrated in the following example:

var aryTest = [1,2];
var aryNew = aryTest.concat(3,[4,5]);
document.write(aryNew);