HTMLGoodies
The ultimate html resource
Earthweb.com


About the Double-Underlined Links


Become a Partner




Search Clipart.com:



internet.commerce















HTML Goodies : Primers : Javascript Primers: The JavaScript Diaries: Part 11

Related Articles
The JavaScript Diaries: Part 10
The JavaScript Diaries: Part 9
The JavaScript Diaries: Part 8
The JavaScript Diaries: Part 7
The JavaScript Diaries: Part 6
The JavaScript Diaries: Part 5
The JavaScript Diaries: Part 4
The JavaScript Diaries: Part 3
The JavaScript Diaries: Part 2
The JavaScript Diaries: Part 1
HTML GOODIES TO GO NEWSLETTER


Other Related Newsletters

The JavaScript Diaries: Part 11


By Lee Underwood

Creating an Array

An array is created using the new keyword. This creates a new instance of the object. Basically this means that we take the array object, make a copy of it and give that copy a name. It's a new "instance" or "occurrence" of the array object. The format for creating a new instance of an array is:

var variableName = new Array();

The format should be pretty familiar to you by now. First, we declared a variable using the var keyword. Then we used the " = " operator to equate the variable to the new instance of the array. Finally, we created a new instance of the array object using the new keyword. Since the array is an object, we placed a semi-colon (" ; ") after it.

The variable creates a reference to the new instance of the array in order to access its data. Remember, a variable is only a data container, so in this case, the variable "contains" a reference to the array to which it is referenced. Let's return to our column of data example. Many times a group of data in a spreadsheet is given a name to make it easier to refer to it in calculations and macros. Instead of writing "=SUM(B7:C16+F7:G15)," which is difficult to remember what it represents, we could give the cells names and write "=SUM(sales + bonus)," That's much easier to understand. When we use a variable to reference an array it's basically the same thing.

There are three basic methods for creating an array and storing data in it. (There are others that we will look at later.) The first two methods shown here use the constructor function.

One method creates an empty instance of an array and then populates it. In this type of array, each element is assigned an index number by the person writing the code, i.e. you.

var fruits = new Array();
  fruits[0]="apples";
  fruits[1]="oranges";
  fruits[2]="peaches";

An array can also be populated by listing each piece of data in a comma-delimited format. This method is called a dense array:

var myCar = new Array("Chevrolet","Honda","Buick","Ford");

In this example each element is given as an argument to the Array object. Remember, objects are created with the format: object();, with the parentheses following it. The parentheses can contain an argument to be used by the object. In this case, it's storing data for use by the Array object. Using this format, the index numbers are assigned automatically by the JavaScript interpreter, beginning with "0."

The difference between the first two different methods is that the first method is good for a large amount of data and the second method is better for a small amount of data. You can see where, using the second method, a large amount of data could become very confusing, i.e.:

var myCar = new Array("Chevrolet","Honda","Buick","Ford","Rolls Royce","Volkswagen","Dodge","Pontiac",
  "Chrysler","Alfa Romeo","Cadillac","Datsun","Fiat","Hyundai","Jeep","Mazda");

as opposed to:

var myCar = new Array()
new myCar[0]="Chevrolet";
new myCar[1]="Honda";
new myCar[2]="Buick";
new myCar[3]="Ford";
new myCar[4]="Rolls Royce";
new myCar[5]="Volkswagen";
new myCar[6]="Dodge";
new myCar[7]="Pontiac";
new myCar[8]="Chrysler";
new myCar[9]="Alfa Romeo";
new myCar[10]="Cadillac";
new myCar[11]="Datsun";
new myCar[12]="Fiat";
new myCar[13]="Hyundai";
new myCar[14]="Jeep";
new myCar[15]="Mazda";

As you can see, the second one is much easier to read. It takes more time, but is worth it in the long run.

The third method for creating an array is by using literal notation. The format is:

var myCar = ["Hyundai","Jeep","Mazda"]

It's similar to the one above except it doesn't use the Array object constructor and it uses brackets instead of parentheses. The Array object constructor is assumed. The JavaScript interpreter knows that the brackets mean to create an array using the given data for the elements. This method can be interpreted by browsers that are version 4.0 and later.

Go to page: Prev  1  2  3  4  5  6  Next  

Tools:
Add htmlgoodies.com to your favorites
Add htmlgoodies.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

IT Management Networking & Communications Web Development Hardware & Systems Software Development Earthwebnews.com

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES