Remove first element from javascript array

The shift() method removes the first array element and "shifts" all other elements to a lower index.

Shifting is equivalent to popping, working on the first element instead of the last.

<button onclick="shiftElement()">Shift Element</button>

<p id="demo"></p>

var courses = ["javascript", "php", "angularjs", "node js"];
document.getElementById("demo").innerHTML = courses;

function shiftElement() {
var shifted_element = courses.shift();
document.getElementById("demo").innerHTML = courses;

  • shift() returns the shifted element.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *