JavaScript - Append Array At The End Of Another Array Last Updated : 28 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different approaches to append an array at the end of another array in JavaScriptpush() Method with Spread Operator - Most UsedIt uses push() combined with the spread operator to append elements of a2 to a1. JavaScript let a1 = [ 1, 2, 3 ]; let a2 = [ 4, 5, 6 ]; a1.push(...a2); console.log(a1); Output[ 1, 2, 3, 4, 5, 6 ] Using concat() MethodIt concatenates a1 and a2 into a new array, preserving the original arrays. JavaScript let a1 = [ 1, 2, 3 ]; let a2 = [ 4, 5, 6 ]; let result = a1.concat(a2); console.log(result); Output[ 1, 2, 3, 4, 5, 6 ] Using Spread OperatorIt combines a1 and a2 by spreading their elements into a new array. JavaScript let a1 = [1, 2, 3]; let a2 = [4, 5, 6]; let result = [...a1, ...a2]; console.log(result); Output[ 1, 2, 3, 4, 5, 6 ] Using for LoopIt iterates over a2 and appends each element to a1 using push(). JavaScript let a1 = [1, 2, 3]; let a2 = [4, 5, 6]; for (let i = 0; i < a2.length; i++) { a1.push(a2[i]); } console.log(a1); Output[ 1, 2, 3, 4, 5, 6 ] Using Array.prototype.reduce()It uses reduce() to get elements from a2 into a copy of a1. JavaScript let a1 = [1, 2, 3]; let a2 = [4, 5, 6]; let result = a2.reduce((acc, val) => { acc.push(val); return acc; }, [...a1]); console.log(result); Output[ 1, 2, 3, 4, 5, 6 ] Using splice() MethodIt appends elements by modifying the original array. The elements of a2 are added at the end of a1 using splice(). JavaScript let a1 = [1, 2, 3]; let a2 = [4, 5, 6]; a1.splice(a1.length, 0, ...a2); console.log(a1); Output[ 1, 2, 3, 4, 5, 6 ] Using Array.from() MethodIt creates a new array by joining existing ones. The elements of a1 and a2 are combined into a new array using Array.from() and the spread operator. JavaScript let a1 = [1, 2, 3]; let a2 = [4, 5, 6]; let result = Array.from([...a1, ...a2]); console.log(result); Output[ 1, 2, 3, 4, 5, 6 ] Which Approach to Use?push() with Spread Operator: Use it for modifying the original array quickly and is efficient for appending smaller arrays.concat() Method: Recommended for immutability when you want to keep the original arrays unchanged.Spread Operator (...): Great for modern, readable, and concise code, especially when creating a new array.for Loop: Suitable when you need custom logic or condition checks during appending.reduce() Method: A more functional approach but may be less readable for simple tasks.splice() Method: Use when you want to modify the original array in-place without creating a new one.Array.from() Method: Provides a flexible way to create a new array when working with different iterable sources. Comment More infoAdvertise with us Next Article JavaScript - Append Array At The End Of Another Array S souravsharma098 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-QnA +1 More Similar Reads JavaScript - Append Array At The Beginning Of Another Array Here are the various approaches to append an array at the beginning of another array in JavaScriptUsing unshift() Method - Most CommonUses the unshift() method to add elements of a2 at the beginning of a1 using the spread operator.JavaScriptlet a1 = [ 4, 5, 6 ]; let a2 = [ 1, 2, 3 ]; a1.unshift(...a 2 min read JavaScript - Append Array at Specific Position of Another Array Here are the different approaches to appending an array at a specific position within another array in JavaScript.Using splice() Method - Most UsedThe splice() method is the most simple approach to insert elements at a specific position. It inserts elements of a2 at index 2 of a1 without removing an 3 min read JavaScript - Add Array to Array of Array Here are the different methods to Array to an Array of Array (Multidimensional Array) in JavaScript1. Using push() MethodThe push() method is one of the most common ways to add an array to an array of arrays. It adds a new element (in this case, an array) to the end of the array.JavaScriptlet mat = 4 min read Extend existing JS array with Another Array To extend an array with another without creating a new array we can use the JavaScript array.push() method. This method extend the array by adding the elements at the end. These are the following methods: 1. Using array.push() with Spread operator JavaScript array.push() method adds one or more elem 2 min read JavaScript- Append in Array These are the following ways to append an element to the JS array: 1. Using Array.push() MethodJavaScript array.push() Method is used to add one or more elements to the end of the array.JavaScriptlet a = [ 10, 20, 30, 40, 50 ]; a.push(90); console.log(a);Output[ 10, 20, 30, 40, 50, 90 ] 2. Using Spr 2 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read JavaScript - Insert Elements at the End of JS Array To insert elements at the end of a JS array, the JavaScript push() method is used.JavaScriptlet a = [10, 20, 30, 40]; a.push(50); console.log(a);Output[ 10, 20, 30, 40, 50 ] Table of ContentUsing Built-In MethodsWriting Your Own MethodUsing Built-In MethodsThe JavaScript push() method is used to ins 1 min read How to Store an Object Inside an Array in JavaScript ? Storing an object inside an array in JavScript involves placing the object as an element within the array. The array becomes a collection of objects, allowing for convenient organization and manipulation of multiple data structures in a single container. Following are the approaches through which it 3 min read JavaScript- Add an Object to JS Array In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively. These are the following ways to add an object to a JS array: Using JavaScript Array 4 min read How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one 2 min read Like