Concatenate Arrays with JavaScript

01/17/2022

Contents

In this article, you will learn how to concatenate arrays with JavaScript.

concat()

The concat() method is a method of an array object that concatenates an array with other arrays and values ​​and returns a new array.

The syntax is as follows.

A new array is created by concatenating array1 and array2.

const newArray = array1.concat(array2);

If you want to concatenate more than two arrays, just add the array as an argument. If you want to concatenate more than two arrays, just add the array as an argument.

const newArray = array1.concat(array2, array3);

Concatenate arrays

In the sample below, use the concat() method to concatenate multiple arrays into one.

const array1 = ['a', 'b', 'c', 'd'];
const array2 = ['e', 'f', 'g', 'h'];
const array3 = ['i', 'j', 'k', 'l'];

const newArray = array1.concat(array2, array3);

console.log(newArray);

//result
//["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]