JavaScript
  Home arrow JavaScript arrow Page 2 - JavaScript Arrays: Pushing, Popping and Sh...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript Arrays: Pushing, Popping and Shifting
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 12
    2006-03-21

    Table of Contents:
  • JavaScript Arrays: Pushing, Popping and Shifting
  • How to append an element to an array using JavaScript: discussion
  • How to pop (or delete at end) an element from an array using JavaScript
  • How to delete an element at the top from an array using JavaScript
  • How to add an element to the top of an array using JavaScript

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    JavaScript Arrays: Pushing, Popping and Shifting - How to append an element to an array using JavaScript: discussion


    (Page 2 of 5 )

    Within the code in the previous section, I mainly created a simple button (which is identified as “ButtonPush”).  The button is defined with an “onclick” event which calls a JavaScript function, “ButtonPush_onclick”.  The same function simply calls another JavaScript function named “Show.”

    The function “Show” is defined as follows:

    function Show()
    {
          var myArray = new Array();
          myArray[0] = "Red";
          myArray[1] = "Green";
          myArray[2] = "Blue";
          myArray[3] = "White";
          document.write("Before adding<br>-------------<br>");
          for (var i = 0; i < myArray.length; i++) 
          {
                document.write(myArray[i] + "<BR>");
          }
          myArray.push("aaa");
          document.write("<br>After adding<br>-------------<br>");
          for (var i = 0; i < myArray.length; i++) 
          {
                document.write(myArray[i] + "<BR>");
          }
    }

    Let us go through the above code part by part.  Let us consider the first part as follows:

          var myArray1 = new Array();
          myArray1[0] = "Red";
          myArray1[1] = "Green";
          myArray1[2] = "Blue";
          myArray1[3] = "White";

    The above part creates a new array named “myArray1” and adds four elements to the same array (“Red,” ”Green,” ”Blue,” and ”White”). 

    We use the following loop to display all the elements (as explained in my first article):

          document.write("Before adding<br>-------------<br>");
          for (var i = 0; i < myArray.length; i++) 
          {
                document.write(myArray[i] + "<BR>");
          }

    The above displays only those four elements we added above (as we did not “push” anything at the moment).  Further proceeding, we have the following:

          myArray.push("aaa");

    The above statement adds a new element, “aaa,” to the array “myArray” at the end.  Once added, we need to check whether it has been added or not.  I repeated the same loop (but with a different heading) as follows:

          document.write("After adding<br>-------------<br>");
          for (var i = 0; i < myArray.length; i++) 
          {
                document.write(myArray[i] + "<BR>");
          }

    More JavaScript Articles
    More By Jagadish Chaterjee


       · Hello. This is another extension to "Javascript Arrays" discussion. You are free...
     

    JAVASCRIPT ARTICLES

    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway