June 13, 2009

0 JavaScript Array Object


The Array Object is used to store a values in a single variable name.

Defining Arrays
-------------------


The Array Object is used to store a values in a single variable name.

We define an array object with the new keyword. The following code line defines an Array object called myArray.

-------------------------------
var myArray=new Array()
--------------------------------


There are two ways of adding values to an array (you can add as many values as you need to define as many variables you require).

1.

-----------------------------------------------------

var mycars=new Array()
mycars [0] = "Saab"
mycars [1] = "Volvo"
mycars [2] = "BMW"

------------------------------------------------------

you could also pass an integer argument to control the array's size:

------------------------------------------------------

var mycars= new Array(3)
mycars [0] = "Saab"
mycars [1] = "Volvo"
mycars [2] = "BMW"


-------------------------------------------------------


2:
--------------------------------------------------------------

var mycars=new Array("Saab", "Volvo", "BMW")

---------------------------------------------------------------

Note: If you specify numbers or true/false values inside the array then the type of variables will be numeric of Boolean instead of string.


Accessing Arrays

You can refer to a particular element in an array be referring to the name of the array and the index number. The index number starts at 0.

The following code line:

document.write(mycars[0])

will result in the following output:
--------------------------------------------

Saab

--------------------------------------------

0 comments:

Post a Comment

Blogger Themes

 
Powered by Blogger