concat()  Array  instance method getterS2C Home  « Globals « Array « concat()

Description

Returns a new array composed of concatenated array(s) and/or value(s), leaving original array(s) unaffected.

  • Any modifications to the old arrays have no affect on the new array created from them.
  • Modifications to an object whose reference is held within the arrays will affect both arrays.

Syntax

Signature Description
anArray.concat(value1, value2, ..., valueN)Returns a new array composed of concatenated array(s) and/or value(s), leaving original array(s) unaffected.

Parameters

Parameter Description
value1, value2, ..., valueNArrays and/or values to concatenate into a new array.

Examples

The code below creates a new array containing the concatenated elements of two other arrays.


// Create an array of weekdays.
var weekDays = ['Mon', 'Tues', 'Wed', 'Thurs'];
// Create an array of weekend days.
var weekendDays = ['Fri', 'Sat', 'Sun'];
// Create an array for all days from above 2 arrays.
var week = weekDays.concat(weekendDays);
alert(week);

Press the button below to action the above code:



The code below creates a new array from an array and some values.


// Create an array of weekdays.
var someDays = ['Mon', 'Tues'];
// Create a new array for week and concatenate values to it.
var allWeekDays = someDays.concat('Wed', 'Thurs');
alert(allWeekDays);

Press the button below to action the above code:


Related Tutorials

JavaScript Intermediate Tutorials - Lesson 1 - Arrays

go to home page Homepage go to top of page Top