JavaScript ES6 – Array concat method
Published:
Jan 15, 2018
2 mins
read
The JavaScript concat()
method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
Think of JavaScript array concat()
method as: “I want to combine 2 or more arrays into one array.”
array concat()
parameters
JavaScript array concat()
method consists of following parameters:
- arrayN – number of arrays to combine to, comma separated array names
Let’s see an example on how to use JavaScript array concat()
method:
const letters = ['a', 'b', 'c'];
const numbers = [1, 2, 3];
const alphaNumeric = letters.concat(numbers);
console.log(alphaNumeric);
// OUTPUT
// [a', 'b', 'c', 1, 2, 3]
use concat()
with multiple arrays
How to concatenate or combine multiple arrays in JavaScript?
To concatenate more than 1 JavaScript arrays we use concat()
method. It accept 1 or more array as arguments, it combines and returns them as a single array.
const arr1 = [5, 9, 2];
const arr2 = [1, 3, 5];
const arr3 = [11, 8, 21];
const combined = arr1.concat(arr2, arr3);
console.log(combined);
// OUTPUT
// [5, 9, 2, 1, 3, 5, 11, 8, 21]
array concat()
usage and tips
- Use
concat()
method to combine 1 or more arrays concat()
combines array and returns the as new array