JavaScript - Insert Element in an array Last Updated : 22 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 returns its new length. JavaScript let a = [10, 20, 30, 40]; let e = 50; a.unshift(e); console.log(a); Output[ 50, 10, 20, 30, 40 ] You can insert an element at the beginning of an array using many other methods. Refer to this article for more methods.At the EndTo add an element to the end of the array, the push() method is widely used. It mutates the array and returns its updated length. JavaScript let a = [10, 20, 30, 40]; let e = 50; a.push(e); console.log(a); Output[ 10, 20, 30, 40, 50 ] You can insert an element at the end of an array using many other methods. Refer to this article for more methods.At a Given PositionTo insert an element at a specific index, the splice() method can be used. This method allows adding elements at any position in the array while optionally removing existing ones. JavaScript let a = [10, 20, 30, 40]; let pos = 2; let e = 50; a.splice(pos, 0, e); console.log(a); Output[ 10, 20, 50, 30, 40 ] You can insert an element at a specific position in an array using many other methods. Refer to this article for more methods.Multiple Elements Using the splice() method, you can also insert multiple elements at a specified index. JavaScript let a = [10, 20, 30, 40]; let pos = 2; let e1 = 50, e2 = 60; a.splice(pos, 0, e1, e2); console.log(a); Output[ 10, 20, 50, 60, 30, 40 ] You can insert multiple elements into an array using many other methods. Refer to this article for more methods. Comment More infoAdvertise with us Next Article How to Insert an element at a specific position in an Array in Java A amit_singh27 Follow Improve Article Tags : JavaScript Web Technologies javascript-array Similar Reads 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 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 How to Insert an element at a specific position in an Array in Java An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in Java.Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos. Approach 1:Â Here's how to do it 4 min read Insert Element at the Beginning of an Array Given an array of integers, the task is to insert an element at the beginning of the array. Examples:Input: arr[] = [10, 20, 30, 40], ele = 50Output: [50, 10, 20, 30, 40]Input: arr[] = [], ele = 20Output: [20]Table of Content[Approach 1] Using Built-In Methods[Approach 2] Using Custom Method[Approac 6 min read How to Insert all the Collection Elements to the Specified Position in Java ArrayList? The element can be inserted at the collection elements to the specified position in ArrayList using Collection.addAll() method which is present in java.util.ArrayList class. If any element present at the index then that element and all its right side elements are shifted to the right side. this meth 3 min read CopyOnWriteArrayList add() method in Java The add(E e) method of CopyOnWriteArrayList inserts the element passed in the parameter to the end of the List or at a specified index in the list. The function returns true on addition of new element to the list. Syntax: public boolean add(E e) or public void add(int index, E element) Parameters: T 2 min read Like