spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / dhtml / column32
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Logo

Array Power, Part II
push()


The push() method
adds one or more elements to the end of an array and returns the new length of the array..

To demonstrate, we'll use our trusty example array called myArray, with three elements:

myArray = [0,"cat",true];, or
myArray = new Array(0,"cat",true);

Now, two new string elements, "dog" and "mouse" are called for. We want to append them to the end of myArray, and we want to store the length of the updated array in a variable called arLength.

Without push()

If the push() method is unavailable, we can append the new elements to the end of the array with these statements:

Statement usedmyArray becomes
1.myArray[myArray.length] = "dog";[true,"cat",0,"dog"]
2.myArray[myArray.length] = "mouse";[true,"cat",0,"dog","mouse"]
3.arLength = myArray.length; 

Not many statements, to be sure. For every element that needs to be added to the array, we append it using myArray[myArray.length], and then get the array's length from its length property and assign it to arLength.

However, it is still not as elegant as using push(), especially if we had many elements to append.

With push()

Using push() we can append the new elements, and get the array length with a single statement:

arLength = myArray.push("dog","mouse");

myArray would then look like this:

[true,"cat",0,"dog","mouse"]

Although the push() method is available in all releases of Navigator 4, its functionality is different in later versions.


Produced by Peter Belesis and

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

All Rights Reserved. Legal Notices.
Created: May 2, 2000
Revised: May 2, 2000

URL: http://www.webreference.com/dhtml/column32/2.html