Thursday 19 January 2012

Access and Modify values of an Array?

Definition: The Array object is used to store multiple values in a single variable.

Example:

First  Create Array (To learn more about how to create array in more detail , Please click the following link) http://arcreview.blogspot.com/2012/01/how-to-create-array-in-javascript.html

var myFruits=new Array(); 
myFruits[0]="Mango";     
myFruits[1]="Orange";
myFruits[2]="Pineapple";

Access an Array

You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0.
The following code line:

document.write(myFruits[0]);

o/p will be:
 Mango

Modify Array 

To modify a value in an existing array, just add a new value to the array with a specified index number.

myFruits[0]="Grapes";

The following code line:
document.write(myFruits[0]);

o/p will be:
Grapes


No comments:

Post a Comment