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 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 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 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 - 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 add elements to an existing array dynamically in JavaScript ? An array is a JavaScript object that can hold multiple values at a time, irrespective of the data type. In this article, we will see how to add new elements to an existing array dynamically in Javascript. We will discuss two methods in this article i.e. push() method and dynamically adding an elemen 4 min read Like