JavaScript Array shift() method : The shift() method is used to remove the first element of the array. The shift() method will return the first element which will be removed. The shift() method will change the length of the array.
Syntax of the shift() method :
array.shift()
Example of Array shift() method : Here I will show you how to remove the first element of the array.
<!DOCTYPE html>
<html>
<body>
<p>To remove first element in the array, click the button "remove first element".</p>
<button onclick="removeFirstElement()">remove first element</button>
<p id="container"></p>
<script>
function removeFirstElement() {
var students = ["Ashok", "Nitin Kumar", "Rajesh", "Pankaj", "Sumit", "Manish", "Pramod"];
var result = students.shift();
document.getElementById("container").innerHTML = students;
}
</script>
</body>
</html>
Output :
Nitin Kumar, Rajesh, Pankaj, Sumit, Manish, Pramod
0 Comment(s)