JavaScript - Insert Elements at the Beginning of JS Array Last Updated : 13 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To insert an element at the beginning of a JS array, the JavaScript unshift() method is used. JavaScript let 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 is used to insert an element at the beginning of the array. JavaScript let a = [10, 20, 30, 40]; // Insert element at the beginning a.unshift(5); console.log("Array after insertion: ", a); OutputArray after insertion: [ 5, 10, 20, 30, 40 ] Writing Your Own MethodTo shift the elements at the beginning of an array, shift the elements of the array to the right by 1 index and insert the new element at the 0th index. JavaScript let a = [2, 3, 4, 5]; let x = 1; // Shift elements to right for (let i = a.length - 1; i >= 0; i--) { a[i + 1] = a[i]; } a[0] = x; console.log(a); Output[ 1, 2, 3, 4, 5 ] Comment More infoAdvertise with us Next Article JavaScript - Insert Elements at the Beginning of JS Array A amit_singh27 Follow Improve Article Tags : JavaScript Web Technologies javascript-array Similar Reads 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 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 - Delete Element from the Beginning of JS Array These are the following ways to delete elements from JavaScript arrays:1. Using shift() method - Mostly UsedThe shift() method removes the first element from the array and adjusts its length.JavaScriptconst a = [1, 2, 3, 4]; // Remove the first element a.shift(); console.log(a);Output[ 2, 3, 4 ] 2. 2 min read JavaScript - Delete Elements from the End of a JS Array These are the following ways to delete elements from the end of JavaScript arrays:1. Using pop() methodThe pop() method is used to remove the last element from an array and returns the modified array.JavaScriptlet a = [1, 2, 3, 4]; // Remove last element a.pop(); console.log(a);Output[ 1, 2, 3 ] 2. 2 min read JavaScript - Insert Multiple Elements in JS Array These are the following ways to insert multiple elements in JavaScript arrays: 1. Using bracket Notation(Simple and Efficient for Small Array)The Bracket can be used to access the index of the given array and we can directly assign a value to that specific index. JavaScriptlet a = [2, 3, 4]; a[3] = 2 min read Like