concat()
Array
instance method
JS Home <<
JS Reference <<
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, ..., valueN | Arrays 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);
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);
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 1 - Arrays