How to insert element based on index in an array.

Learn how to insert an element in specified index of the array in Javascript.

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in-place(in original array).


The splice method will take three argument β†’ startΒ , deleteCount(optional), items to add (optional)Β .

In the above program we are deleting 0 elements from index 2 and adding 3 into 2 index.

We can also add multiple elements into itΒ .

var array = [1,2,4,5];
array.splice(2, 0, 3, 4, 3, 4); // [1,2,3,4,3,4,4,5]

If you find this helpful surprise 🎁 me here.

Share if you feel happy πŸ˜ƒ πŸ˜† πŸ™‚Β .

Follow Javascript JeepπŸš™ if you feel worthy.

Leave a comment

Design a site like this with WordPress.com
Get started