Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()

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

    Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()

    push() and pop(), these two methods append elements to an array and remove an element from the an array respectively. Both of these methods work at the end of the array, where the index is largest. Javascript arrays have native support for these two methods.

    Unshift() and shift(), Javascript also has support for parallel methods that work on the beginning of the array, where the index is smallest. Unshift() and shift() are basically the same as push() and pop(), only, at the other end of the array.

    Javascript Array: Push() Method

    Push data onto the array. Push() appends elements to the end of the given array. Note that it can take more than one argument, each of which is individually appended to the array. In the output, notice that when push() takes multiple arguments they are appended in a left-to-right order (mimicing their appearence in the arguments list).

    // Build an array of test data. 
    var data = [ "Z" ];
    
    data.push( "Aa" );
    data.push( "Bb", "Cc" );
    
    // Output resultant array.
    console.log( data );
    
    OUTPUT:
    ["Z", "Aa", "Bb", "Cc"]
    

    Javascript Array: Pop() Method

    The pop() method pulls the last element off of the given array and returns it. This alters the array on which the method was called.

    // Build an array of test data.
    var data = [ "Aa", "Bb", "Cc" ];
    
    // Pop the element off of the end of the array.
    console.log( data.pop() );
    console.log( data );
    
    OUTPUT:
    Cc
    ["Aa", "Bb"]
    

    Javascript Array: Unshift() Method

    The unshift() method is like the push() method, only it works at the beginning of the array. The unshift() method can prepend one or more elements to the beginning of an array. This alters the array on which the method was called. In the output, notice that when unshift() takes multiple arguments, they are prepended in a right-to-left order (mimicing their appearence in the arguments list).

    // Build an array of test data.
    var data = [ "Z" ];
    
    data.unshift( "Aa" );
    data.unshift( "Bb", "Cc" );
    
    // Output resultant array.
    console.log( data );
    
    OUTPUT:
    ["Bb", "Cc", "Aa", "Z"]
    

    Javascript Array: Shift() Method

    The shift() method is like the pop() method, only it works at the beginning of the array. The shift() method pulls the first element off of the given array and returns it. This alters the array on which the method was called.

    // Build an array of test data.
    var data = [ "Aa", "Bb", "Cc" ];
    
    // Shift the element off of the beginning of the array.
    console.log( data.shift() );
    console.log( data );
    
    OUTPUT:
    Aa
    ["Bb", "Cc"]
    
    Javascript

 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: