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, ..., 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

JavaScript Basics

JavaScript Basics

JavaScript Intermediate

JavaScript Intermediate

JavaScript Advanced

JavaScript Advanced

JavaScript Reference

JavaScript Entities

Globals

Array

Class Properties
prototype
Instance Properties
constructor
length
Class Methods
None.
Instance Methods Getters
concat()
join()
slice()
toString()
Instance Methods Setters
pop()
push()
reverse()
shift()
sort()
splice()
unshift()

Statements

Operators