JavaScript Arrays – How to Remove Element from an Array

arraysjavascript

var arr = [1,2,3,5,6];

Remove the first element

I want to remove the first element of the array so that it becomes:

var arr = [2,3,5,6];

Remove the second element

To extend this question, what if I want to remove the second element of the array so that it becomes:

var arr = [1,3,5,6];

Best Answer

shift() is ideal for your situation. shift() removes the first element from an array and returns that element. This method changes the length of the array.

array = [1, 2, 3, 4, 5];

array.shift(); // 1

array // [2, 3, 4, 5]
Related Question