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 Like