SunQuest
 
       JavaScript
  Home arrow JavaScript arrow Page 3 - JavaScript Arrays: Pushing, Popping and Sh...
IBM developerWorks
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 pop (or delete at end) an element from an array using JavaScript


    (Page 3 of 5 )

    In this section I focus on “popping” (also called “deleting” at the end) an element from an array.

    Now, let us try to develop a simple script (JavaScript) which deletes (or pops) a single element at the end of an array.  Have a look at the following code:

    <html>
          <head>
                <meta  name=vs_targetSchema  content="http://schemas.microsoft.com/intellisense/ie5">
    <script  id="clientEventHandlersJS"  language="javascript">
    <!--
    function Show()
    {
          var myArray = new Array();
          myArray[0] = "Jag";
          myArray[1] = "Chat";
          myArray[2] = "Win";
          myArray[3] = "Dhan";
          document.write("Before deleting<br>-------------<br>");
          for (var i = 0; i < myArray.length; i++) 
          {
                document.write(myArray[i] + "<BR>");
          }
          myArray.pop();
          document.write("<br>After deleting<br>-------------<br>");
          for (var i = 0; i < myArray.length; i++) 
          {
                document.write(myArray[i] + "<BR>");
          }
    }

    function Button1_onclick() {
          Show();
    }
    //-->
                </script>
          </head>
          <body>
          <form  id="form1">
                      <input  type="button"  value="Show"  id="Button1"  name="Button1"  onclick="return Button1_onclick()">
                </form>
          </body>
    </html>

    When the above code is executed we get the following output:

    Before deleting
    -------------
    Jag
    Chat
    Win
    Dhan

    After deleting
    -------------
    Jag
    Chat
    Win

    The above code is very similar to the code available in the first section with only the following change:

          myArray.pop();

    Earlier, in my previous example (in the first section), I used the “push” method.  The “push” method accepts a parameter as an element to add.  Coming to the “pop” method, it doesn’t accept any parameter to delete.  That means when you issue “pop,” the last element (or the latest element added at the end of the array) gets deleted from the array automatically.  Apart from this difference, the rest of the code is quite similar.

    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 5 hosted by Hostway