JavaScript - Insert Element in an array Last Updated : 22 Nov, 2024 Comments Improve Suggest changes 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 JavaScript - Insert Element in an array 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 item at the beginning of an array in PHP ? Arrays in PHP are a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed 5 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 How to Add an Element to an Array in Java? In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array. In this article, we will discuss the NewDifferent Ways to Add an Element to an ArrayThere are two different approaches we c 3 min read Sort an Array and Insert an Element Inside Array in Java Sorting an array can be done by using inbuilt sort function while for the insertion we have to create a new array to do so as arrays in Java are immutable. To learn more about sorting in Java follow the article mentioned below: Sorting: Arrays.sort() in Java with examples Approach 1: Create a new ar 3 min read How to Fill (initialize at once) an Array in Java? An array is a group of like-typed variables that are referred to by a common name. In this, article we will learn about Filling array in Java while Initialization.Example:Java// Java program to fill the element in an array import java.util.*; public class Geeks { public static void main(String args[ 3 min read Like