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>

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

function shiftElement() {
var shifted_element = courses.shift();
console.log(shifted_element);
document.getElementById("demo").innerHTML = courses;
}
</script>

  • shift() returns the shifted element.

Share This:

Leave a Reply

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