JavaScript - Append Array At The Beginning Of Another Array Last Updated : 28 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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. JavaScript let a1 = [ 4, 5, 6 ]; let a2 = [ 1, 2, 3 ]; a1.unshift(...a2); console.log(a1); Output[ 1, 2, 3, 4, 5, 6 ] Using concat() MethodThe concat() method creates a new array without modifying the existing arrays. JavaScript let a1 = [ 4, 5, 6 ]; let a2 = [ 1, 2, 3 ]; let result = a2.concat(a1); console.log(result); Output[ 1, 2, 3, 4, 5, 6 ] Using Spread OperatorCreates a new array by spreading elements of a2 first, followed by a1. JavaScript let a1 = [ 4, 5, 6 ]; let a2 = [ 1, 2, 3 ]; let result = [...a2, ...a1 ]; console.log(result); Output[ 1, 2, 3, 4, 5, 6 ] Using splice() MethodModifies the original array in-place. It inserts elements of a2 at the beginning of a1 using splice() with 0 as the start index. JavaScript let a1 = [ 4, 5, 6 ]; let a2 = [ 1, 2, 3 ]; a1.splice(0, 0, ...a2); console.log(a1); Output[ 1, 2, 3, 4, 5, 6 ] Using for LoopIt iterates over a2 in reverse order, adding each element to the start of a1 using unshift(). JavaScript let a1 = [ 4, 5, 6 ]; let a2 = [ 1, 2, 3 ]; for (let i = a2.length - 1; i >= 0; i--) { a1.unshift(a2[i]); } console.log(a1); Output[ 1, 2, 3, 4, 5, 6 ] Which Approach to Use?unshift() Method: Use this when you need to modify the original array directly and you want a simple and quick solution. It is efficient for small arrays but may have performance impacts for larger arrays due to frequent element shifting.concat() Method: Best for creating a new array without altering the original ones. Ideal for situations where immutability and data integrity are important.Spread Operator (...): Recommended for concise, readable code when creating new arrays. It’s great when you don’t want to mutate existing arrays and prefer modern JavaScript syntax.splice() Method: Useful when you need to insert elements at specific positions in an array. Be cautious, as it modifies the original array.for Loop: Offers flexibility and manual control over how elements are added. It’s useful when complex logic or condition checks are needed during the addition. Comment More infoAdvertise with us Next Article JavaScript - Append Array At The Beginning 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 End Of Another Array 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.JavaScriptlet a1 = [ 1, 2, 3 ]; let a2 = [ 4, 5, 6 ]; a1.push(...a2); console.lo 3 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 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 - 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 JavaScript - Insert Elements at the Beginning of JS Array To insert an element at the beginning of a JS array, the JavaScript unshift() method is used.JavaScriptlet a = [1,2,3,4] // Inserting element a.unshift(0) console.log(a)Output[ 0, 1, 2, 3, 4 ] Table of ContentUsing Built-in MethodsWriting Your Own MethodUsing Built-in MethodsThe JS unshift() method 1 min read Like