Skip to content
the poor coder

javascript append array to array

Poor Man
Published
Reading time
2 min read
On this page

JavaScript: How to Append Arrays to Arrays#

Appending arrays in JavaScript is a common task that can be accomplished in different ways depending on your needs. Here are some methods to append arrays to arrays in JavaScript:

Method 1: Concatenation Operator#

The easiest way to append arrays in JavaScript is by using the concatenation operator (+). This operator concatenates two or more arrays and returns a new array without changing the original ones.

Text

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = arr1 + arr2;

console.log(arr3); // Output: "1,2,34,5,6"

In the above example, we have declared two arrays (arr1 and arr2) and concatenated them using the + operator. However, the result is a string that contains all the elements of both arrays separated by commas. If you want to append arrays as arrays rather than strings, you need to use another method.

Method 2: Array.concat() Method#

The Array.concat() method is another way to append arrays in JavaScript. This method creates a new array that contains the elements of the original arrays. You can pass one or more arrays as arguments to the concat() method.

Text

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = arr1.concat(arr2);

console.log(arr3); // Output: [1, 2, 3, 4, 5, 6]

In the above example, we have declared two arrays (arr1 and arr2) and concatenated them using the concat() method. The result is a new array (arr3) that contains all the elements of both arrays.

Method 3: Spread Operator#

The spread operator (...) is a newer feature in JavaScript that makes it easier to append arrays. This operator allows you to spread the elements of an array into another array. You can use the spread operator to append one or more arrays to another array.

Text

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = [...arr1, ...arr2];

console.log(arr3); // Output: [1, 2, 3, 4, 5, 6]

In the above example, we have declared two arrays (arr1 and arr2) and used the spread operator (...) to append them to a new array (arr3). This method is concise and easy to read.

Conclusion#

In conclusion, there are different ways to append arrays to arrays in JavaScript. The concatenation operator (+), Array.concat() method, and spread operator (...) are three popular methods. Depending on your needs, you can choose the method that suits you best.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC