The function of an array object is to store a series of values using a separate variable name.
Create an array and assign it a value:
Instance
var mycars = new Array ();
Mycars[0] = "Saab";
MYCARS[1] = "Volvo";
MYCARS[2] = "BMW";
What is an array?
an Array object uses a separate variable name to store a series of values.
If you have a set of data (for example: car name), there are individual variables as follows:
var car1= "Saab";
var car2= "Volvo";
var car3= "BMW";
However, if you want to find out about a particular car? And not 3 cars, but 300 cars? This will not be an easy thing!
The best way is to use arrays.
An array can store all values with a variable name, and you can access any value with the variable name.
Each element in the array has its own ID so that it can be accessed easily.
Create an array
There are three methods of creating an array.
The following code defines an array object named Mycars:
1: Conventional way:
var mycars=new Array ();
Mycars[0]= "Saab";
Mycars[1]= "Volvo";
Mycars[2]= "BMW";
2: Concise Way:
var mycars=new Array ("Saab", "Volvo", "BMW");
3: literal:
var mycars=["Saab", "Volvo", "BMW"];
accessing arrays
you can access a particular element by specifying the array name and the index number.
The following instance can access the first value of the Mycars array:
The following instance modifies the first element of the array Mycars:
Lamp [0] is the first element of the array. [1] is the second element of the array.
You can have different objects in an array.
all JavaScript variables are objects. An array element is an object. function is an object.
So you can have different types of variables in the array.
You can include object elements, functions, and arrays in an array:
Myarray[0]=date.now;
myarray[1]=myfunction;
Myarray[2]=mycars;
Array methods and properties
use array objects to predefined properties and methods:
var x=mycars.length //The number of elements in Mycars
var y=mycars.indexof ("Volvo") //The index Positio N of "Volvo"
Create a new method
The prototype is a JavaScript global constructor. It can build properties and methods for new JavaScript objects.
Instance: Creates a new method.
Array.prototype.ucase=function ()
{for
(i=0;i<this.length;i++)
{this[i]=this[i].touppercase ()
}}
The example above creates a new array method for converting an array lowercase character to uppercase.