11, Jan 2023
How to splice an element to the start of an array?
To splice an element to the start of an array, you can use the
splice() method with the following arguments:
array.splice(0, 0, element);
This will add the
element to the start of the
array, without removing any elements.
let array = [1, 2, 3]; array.splice(0, 0, 0); console.log(array); // [0, 1, 2, 3]
The first argument (
0) is the index at which to start inserting the new element. The second argument (
0) is the number of elements to remove, starting at the index specified in the first argument. The third argument (
element) is the element to insert at the specified index.