[JavaScript] How to insert an element at a specific location in an array?


I want to insert an element at a specific index of an array, how to do that in Javascript?

1 Answer

3 years ago by

You can use splice() function to add elements at the specific index of an array

let numbers = [1,2,3,4,5,6,7,8,9];
let index=9;
numbers.splice(index, 0, 10);  //Syntax: array.splice(index, howmany items to be removed, item1, ....., itemn)

Try checking the result here: https://onecompiler.com/javascript/3xmvxpx77

3 years ago by Meera