JavaScript - Insert Elements at a Given Position in an JS Array Last Updated : 13 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To insert an element at a specific position in a JavaScript array, the JS splice() method is used. JavaScript let a = [10, 20, 30, 40]; let e = 50; let i = 2; a.splice(i - 1, 0, e); console.log(a); Output[ 10, 50, 20, 30, 40 ] Table of ContentUsing built-in MethodWriting Your Own MethodUsing built-in MethodThe JavaScript splice() method is used to insert elements at a specific position in JS Array. JavaScript let a = [10, 20, 30, 40]; a.splice(4, 0, 50); console.log(a); Output[ 10, 20, 30, 40, 50 ] Writing Your Own MethodTo add an element at a given index in an array, shift all the elements from that index to the right and then insert the new element at the required index. JavaScript let a = [10, 20, 30, 40]; let e = 50; let p = 2; // Shifting elements to the right for (let i = a.length; i >= p; i--) a[i] = a[i - 1]; // Insert the new element at index pos - 1 a[p - 1] = e; console.log(a); Output[ 10, 50, 20, 30, 40 ] Comment More infoAdvertise with us Next Article JavaScript - Insert Elements at a Given Position in an 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 JavaScript - Insert Element in an array In JavaScript elements can be inserted at the beginning, end, and at any specific index. JS provides several methods to perform the operations.At the Beginning This operation inserts an element at the start of the array. The unshift() method is commonly used, which mutates the original array and ret 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 JavaScript - Insert Character at a Given Position in a String These are the following approaches to insert a character at a given position: 1. Using String Concatenation with SlicingSplit the string into two partsâbefore and after the positionâand concatenate the character in between.JavaScriptlet str = "Hello GFG"; let ch = "!"; let idx = 5; let res = str.sli 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 Like