Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pop and Push Elements in an Array

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 439
    Comment on it

    The examples below will help how one can include or remove new array elements in javascript with the help of unshift/ push/ shift/ pop functions.

    unshift() is used to add one or more elements to the starting of the existing array and returns the current length of the Array.
    
    Example:
    
    var data = [ "1", "2" ];
    var newdata = data.unshift ( "88", "99" );
    alert ( data );    // displays "88,89,1,2"
    alert ( newdata ); // retruns length "4" elements
    
    push() is used to add one or more elements to the end of the existing array
    
    Example:
    
    var data = [ "1", "2" ];
    var newdata = data.push ("88", "99" );
    alert ( data );    // displays "1, 2, 88, 99"
    alert ( newdata ); // retruns length "4" elements
    
    
    
    shift() is used to remove first element from the existing array and returns the removed element.
    
    Example:
    
    var data = [ "1", "2", "88", "99" ];
    var newdata = data.shift( );
    alert ( data ); // displays "2, 88, 99" 
    alert ( newdata );  // returns "1"
    
    
    pop() is used to remove the last element from the existing array and returns the removed element.
    
    Example:
    
    var data = [ "1", "2", "88", "99" ];
    var newdata = data.pop( );
    alert ( data ); // displays "1,2,88" 
    alert ( newdata );  // returns "99"
    

 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: