Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • shift() method of the JavaScript Array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 126
    Comment on it

    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 :

    1. array.shift()


    Example of Array shift() method : Here I will show you how to remove the first element of the array.

    1. <!DOCTYPE html>
    2. <html>
    3. <body>
    4.  
    5. <p>To remove first element in the array, click the button "remove first element".</p>
    6. <button onclick="removeFirstElement()">remove first element</button>
    7. <p id="container"></p>
    8.  
    9. <script>
    10. function removeFirstElement() {
    11. var students = ["Ashok", "Nitin Kumar", "Rajesh", "Pankaj", "Sumit", "Manish", "Pramod"];
    12. var result = students.shift();
    13. document.getElementById("container").innerHTML = students;
    14. }
    15. </script>
    16.  
    17. </body>
    18. </html>

    Output :

    1. Nitin Kumar, Rajesh, Pankaj, Sumit, Manish, Pramod

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: