[JavaScript] How to insert an element in the starting of the array?


I want to insert a new element at the starting of an array, how to do that in Javascript?

1 Answer

3 years ago by

You can use splice() function to add elements to an array

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

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

3 years ago by Meera