Whenever you use concat() on an array, the method does the following:
- It concatenates (merges) its calling array with its arguments.
- It creates and populates a new array with the merged values.
Syntax of the concat()
Method
concat()
accepts one or more arguments. Here is the syntax:
The item
arguments are the values you wish to concatenate into the callingArray
.
Examples
Below are examples of the concat()
method.
How to merge an array with another array
Try it on StackBlitz
The snippet above used concat()
to concatenate (merge) array1
with array2
.
How to merge an array with three other arrays
Try it on StackBlitz
The snippet above used concat()
to concatenate four arrays.
How to merge an array with a nested array
Try it on StackBlitz
The snippet above used concat()
to merge array1
with a nested array.
CodeSweetly adsExpress Your Love for Coding!
Explore CodeSweetly's Shop for an array of stylish products to enhance your coding experience.
Shop now
How to merge two nested arrays
Try it on StackBlitz
The snippet above used concat()
to merge two nested arrays.
How to merge an array with a number
Try it on StackBlitz
The snippet above used concat()
to merge array1
with a number.
How to merge an array with three numbers
Try it on StackBlitz
The snippet above used concat()
to merge array1
with three numbers.
How to merge an array with two strings
Try it on StackBlitz
The snippet above used concat()
to merge array1
with two strings.
How to merge an array with a number, string, and another array
Try it on StackBlitz
The snippet above used concat()
to concatenate array1
with three other items.
How to merge an array with an object
Try it on StackBlitz
The snippet above used concat()
to merge array1
with an object.