JavaScript - Insert Multiple Elements in JS Array Last Updated : 15 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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. JavaScript let a = [2, 3, 4]; a[3] = 1; a[4] = 5; console.log(a); Output[ 2, 3, 4, 1, 5 ] 2. Using splice() Method (Efficient for Large Array)The splice() method can be used to insert elements to the given array, as it takes parameter where you have to add the position where you want to insert the element, the number of elements you want to remove(in this cse it is 0 as we are adding elements) and then the value that you want to insert. JavaScript let a = [10, 20, 30, 40]; a.splice(a.length, 0, ...[50, 60, 70]); console.log(a); Output[ 10, 20, 30, 40, 50, 60, 70 ] 3. Using spread Operator(Efficient for Large Array)This method creates a new array to insert elements. The JavaScript spread syntax (...) can be used to add multiple elements in the JS array. we can directly assign the array values to the new array. JavaScript let a1 = [2, 3]; // Inserts 0 at the start let a2 = [0, 1, ...a1]; console.log(a2); // Inserts 4,5 at the end let a3 = [...a1, 4, 5]; console.log(a3); Output[ 0, 1, 2, 3 ] [ 2, 3, 4, 5 ] Comment More infoAdvertise with us Next Article JavaScript - Insert Multiple Elements in 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 Elements at a Given Position in an JS Array To insert an element at a specific position in a JavaScript array, the JS splice() method is used. JavaScriptlet 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 1 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 - Inserting Multiple Items at Specific Index in JS Array We can insert an item into Array at a specific index using the JavaScript Array splice method. This method removes the elements from the mentioned index and add items at that specific position.1. Using array.splice() MethodJavaScript array.splice() Method is an inbuilt method in JavaScript that is u 3 min read JavaScript - Insert Character in JS String In JavaScript, characters can be inserted at the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.At the BeginningTo insert a character at the beginning of a string, you can use string concatenation or template literals 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 Types of Arrays in JavaScript A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi 3 min read JavaScript - Insert a String at a Specific Index These are the following methods to insert a string at a specific index in JavaScript:1. Using the slice() MethodInserting a string using the slice() method involves splitting the original string into two parts: one before the insertion point and one after. The new string is then placed between these 3 min read JavaScript - Replace an Item from JS Array These are the following ways to replace an item from the JS array: 1. Using Array IndexingThe Array Indexing approach replaces an item in an array by directly accessing its position using the index and assigning a new value. This method is straightforward, modifies the original array, and is ideal f 2 min read Like