over 9 years ago
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 :
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>
<!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 :
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)