How to merge arrays in JavaScript

Today we’ll explain to you how to merge arrays in JavaScript. Here we show you multiple ways to merge two or more arrays and return a new combined array.

Ways to merge arrays in JavaScript

  1. concat() method
  2. Spread operator
  3. push() method

1. concat() method

It’s a simple JavaScript method to combine two or more arrays. This method merge the arrays and returns the new array.

Let’s take an example, suppose we have two arrays like below.

2. Spread operator

Spread operator is introduced by JavaScript ES6. Spread operator also concatenates the array values and returns the new array.

3. push() method

Using push() method, you can merge the multiple arrays as below.

Here, we use the push() method with spread operator to add the elements of the all arrays into a defined combinedArray array.

If you don’t use spread operator then the whole array will be pushed into a defined array like below.

That’s it for today.
Thank you for reading. Happy Coding..!!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *