How To Append One Array To Another In JavaScript?

How to Append one Array to Another in JavaScript

There are many ways to append one array to another in JavaScript. In the article, I will show you some ways to go through some of the built-in JavaScript functions or methods so that you can understand and implement them. I have used some of the most concise and easy-to-understand examples.

How did I append one array to another in JavaScript?

Using the loop For and push() method

When working with arrays, we often use loops in which the For loop is used the most, so to append one array to another in Javascript, we can also use the For loop to perform.

Before I do, I will help you understand how the push() method of arrays works and how to do it.

Syntax

array.push(item1, item2, ..., itemX)

Parameter

  • item1, item2, …, itemX: These are the elements you want to add to your array

Here I will implement using For loop and push() method to add two strings as follows:

const nums1 = [3, 4, 6, 8];
const nums2 = [1, 2, 9, 5, 7];

// Using For loop and push() method
for (let index = 0; index < nums2.length; index++) {
	nums1.push(nums2[index]);
}

console.log(nums1);

Output:

(9) [3, 4, 6, 8, 1, 2, 9, 5, 7]

I append the array ‘nums2’ to the string ‘nums1’ by traversing the elements of ‘nums2’ using a For loop and then passed them to the push() method to add each element of ‘nums2’ to ‘nums1’ ‘.

Using spread(…) operator

spread(…) operator expands an iterable into more elements. We can use it to concatenate two arrays combined with the push() method or without any method. It can also append one array to another okay.

When spread(…) operator combined with push() method we will do the following:

const nums1 = [3, 4, 6, 8];
const nums2 = [1, 2, 9, 5, 7];

// Using spread(...) operator and push() method
nums1.push(...nums2);
console.log(nums1);

Output:

(9) [3, 4, 6, 8, 1, 2, 9, 5, 7]

Or we can use it without any other methods like this:

const nums1 = [3, 4, 6, 8];
const nums2 = [1, 2, 9, 5, 7];

// Using spread(...) operator
const nums = [...nums1, ...nums2];
console.log(nums);

Output:

(9) [3, 4, 6, 8, 1, 2, 9, 5, 7]

We have many ways to use the spread(…) operator to concatenate two arrays, and both achieve the results we expect.

Using the concat() method

concat() method is a built-in method to concatenate two arrays by simply passing the array you want to concatenate as its parameter.

Syntax

array1.concat(array2, array3, ..., arrayX)

Parameter

  • array2, array3, …, arrayX: One or more arrays you want to concatenate.

Example:

const nums1 = [3, 4, 6, 8];
const nums2 = [1, 2, 9, 5, 7];

// Using the concat() method
const nums = nums1.concat(nums2, [0, 10, 11]);
console.log(nums);

Output:

(12) [3, 4, 6, 8, 1, 2, 9, 5, 7, 0, 10, 11]

With the concat() method, we can combine multiple strings in just a few quick and easy steps.

Summary

Above are some common ways when we want to append one array to another in JavaScript. When we use the concat() method, the string concatenation is no longer a problem. In return, it is straightforward and faster. I hope the article helps you and your program. Good luck.

Maybe you are interested:

Leave a Reply

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