How to Merge Two Arrays Without Creating a New Array in JavaScript? Last Updated : 07 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Given two arrays, the task is to merge both arrays to make a single array without creating a new array in JavaScript. It modifies one array in place to store the merged array elements.We can use the array push() method to insert array elements into another array using Spread Operator. This approach will modify the first array in place and won't create a new array.Syntaxarray1.push( ...array2); JavaScript let array1 = [ 1, 2, 3 ]; let array2 = [ 4, 5, 6 ]; array1.push(...array2); console.log(array1); Output[ 1, 2, 3, 4, 5, 6 ] Please Read JavaScript Array Tutorial for complete understanding of JavaScript Array.Table of ContentUsing Array concat() MethodUsing Array splice() MethodUsing a for LoopUsing Array concat() MethodWe can use array concat() method to Merge Two Arrays Without Creating a New Array. It is a very basic method to merge arrays. It takes second array as a parameter and store the merged array to the first array. Syntaxarray1 = array1.concat( array2 ); JavaScript let array1 = [ 1, 2, 3 ]; let array2 = [ 4, 5, 6 ]; // Store the merged array to first array array1 = array1.concat(array2); console.log(array1); Output[ 1, 2, 3, 4, 5, 6 ] Using Array splice() MethodThe array splice() method is used to modify the contents of an array by removing the existing elements and/or by adding new elements.Syntaxarray1.splice( index, removeCount, ...array2 ) JavaScript let array1 = [ 1, 2, 3 ]; let array2 = [ 4, 5, 6 ]; array1.splice(array1.length, 0, ...array2); console.log(array1); Output[ 1, 2, 3, 4, 5, 6 ] Using a for LoopWe can also use for loop to merge two arrays without creating a new array. We will use for loop with push() method to merge arrays without creating new array. JavaScript let array1 = [ 1, 2, 3 ]; let array2 = [ 4, 5, 6 ]; for (let i = 0; i < array2.length; i++) { array1.push(array2[i]); } console.log(array1); Output[ 1, 2, 3, 4, 5, 6 ] Comment More infoAdvertise with us Next Article How to Merge Two Arrays Without Creating a New Array in JavaScript? G geethabtrweh Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-Program Similar Reads JavaScript Program to Merge two Sorted Arrays into a Single Sorted Array In this article, we will cover how to merge two sorted arrays into a single sorted array in JavaScript. Given there are two sorted arrays, we need to merge them into a single sorted array in JavaScript. We will see the code for each approach along with the output. Below are the possible approaches t 3 min read JavaScript Program to Transform Nested Array into Normal Array JavaScript allows us to convert nested arrays into a single, flat array using JavaScript. By simplifying nested structures, you can enhance the manageability and readability of your code. Example: Input: [[1, 2], [3, [4, 5]]] Output: [1, 2, 3, 4, 5]Below are the approaches to transform nested arrays 3 min read JavaScript Program to Create an Array of Unique Values From Multiple Arrays Using Set Object We have given multiple arrays consisting of numerical values, and our task is to create an output array that consists of unique values from the multiple input arrays. We will use the Set object in JavaScript language. Example: Input: inputarray1: [1,2,3,4,5] inputarray2: [4,5,6,7,8] Output: outputAr 5 min read JavaScript Program to Find if Arrays are Disjoint or Not Given two arrays, Our task is to check if the given two arrays are disjoint or not. Disjoint arrays in JavaScript are arrays that have no elements in common Input: arr1[] = [12, 34, 11, 9, 3] arr2[] = [2, 1, 3, 5]Output: Not Disjoint3 is common in two sets.Input: arr1[] = [12, 34, 11, 9, 3] arr2[] = 3 min read How to Add a key/value Pair to Map in JavaScript ? This article will demonstrate how we can add a key-value pair in the JavaScript map. JavaScript Map is a collection of key-value pairs in the same sequence they are inserted. These key values can be of primitive type or the JavaScript object. All methods to add key-value pairs to the JavaScript Map: 3 min read JavaScript Program to find Intersection of Unsorted Arrays JavaScript arrays are versatile structures used to store collections of elements. Oftentimes, these arrays may contain duplicates, and finding the common elements among them, known as the "intersection," becomes necessary. In this article, we'll explore various methods to efficiently find the inters 3 min read Sum of Middle Elements of Two Sorted Arrays using JavaScript Given 2 sorted arrays Array1 and Array2 of size n each. Merge the given arrays and find the sum of the two middle elements of the merged array. Example: Input: array1 = [1, 3, 5] array2 = [2, 4, 6] n = 3 Output: 7 Explanation: Given two sorted arrays ar1 and ar2 of size n each: ar1 = [1, 3, 5] and a 5 min read JavaScript Program for Merge Sort What is Merge Sort Algorithm?Merge sort is one of the sorting techniques that work on the divide and conquer approach. The Given array is divided in half again and again and those parts are arranged in sorted order and merged back to form the complete sorted array. Working of Merge SortTo Understand 3 min read How to Merge/Flatten an array of arrays in JavaScript ? Merging or flattening an array of arrays in JavaScript involves combining multiple nested arrays into a single-level array. This process involves iterating through each nested array and appending its elements to a new array, resulting in a flattened structure.To Merge or flatten array we can use arr 3 min read How to Create Nested Arrays from a Nest of Arrays in JavaScript ? Creating nested arrays from a nest of arrays in JavaScript involves organizing multiple arrays into a hierarchical structure, which is often useful for managing and representing complex data relationships. This process entails encapsulating arrays within other arrays to form multi-dimensional struct 3 min read Like