How to use concat with Assignment In Javascript
The concat method is typically used to merge arrays and returns a new array. However, by assigning the result back to the original array, we can merge the arrays without explicitly creating a new array.
Example: In the below example, we are using concat with assignment to merge two arrays.
let array1 = ['Red', 'Blue', 'Green'];
const array2 = ['Yellow', 'Purple', 'Orange'];
array1 = array1.concat(array2);
console.log(array1);
Output
[ 'Red', 'Blue', 'Green', 'Yellow', 'Purple', 'Orange' ]
JavaScript Program to Merge Two Arrays Without Creating a New Array
In this article, we will learn how to merge two arrays without creating a new array. In JavaScript Array is used to store elements of similar data types. If we want to add an element into another array for merging two arrays, we can use the push method. There are many methods for merging two arrays without creating a new array.
Table of Content
- Method 1: Using the push() method
- Method 2: Using splice() Method
- Method 3: By using push.apply method
- Method 4: Using concat with Assignment