Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between shift(), unshift() and push(),pop() methods

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 6.51k
    Comment on it

    Writing a JavaScript code in correct approach can be fun instead of terrifying. In this tutorial we will see shift(), unshift() and push(), pop() are the methods of JavaScript that allows us to add remove elements. 
    Lets discuss all these metnods one by one :

    Shift() :

    This method is similar to pop() method, the only difference is that it works at the beginning of the array. It delete the first element from the given array and returns it.

    Example :

    // Creating a test array
    var data = [ "A", "B", "C" ];
    // Delete the element from the beginning of the array.
     data.shift();
    

    Unshift() :

    This method is similar to push() method, the only difference is that it works at the beginning of the array. It add one or more elements to the beginning of an array.

    Example :

    // Creating a test array
    var data = [ "X" ];
    // Add the element at the beginning of an array.
    data.unshift( "B", "C" ); 
    

    Push() :

    This method can add one or more elements at the end of an array.

    Example :

    // Creating a test array
    var data = [ "X" ];
    // Add the element at the end of an array.
    data.push( "B", "C" ); 
    

    Pop() :

    This method removes the last element from the given array and returns it.

    Example :

    // Creating a test array
    var data = [ "A", "B", "C" ];
    // Delete the element from the end of an array.
    data.pop();
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: