Javascript array
The Array is used to store multiple values in a single variable.
What is an Array?
An array is a special variable, which can hold more than one value, at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables look like this:
What is an Array?
An array is a special variable, which can hold more than one value, at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables look like this:
cars1="Honda"; cars2="Suzuki"; cars3="BMW";
Create an Array
An array can be defined in three ways.
The following code creates an Array object called myCars:
An array can be defined in three ways.
The following code creates an Array object called myCars:
1. var myCars=new Array();
// regular array(add an optional integer
// argument to control array’s size)
myCars[0]=”Honda”;
myCars[1]=”Suzuki”;
myCars[2]=”BMW”;
// regular array(add an optional integer
// argument to control array’s size)
myCars[0]=”Honda”;
myCars[1]=”Suzuki”;
myCars[2]=”BMW”;
2. var myCars=new Array(“Honda”,”Suzuki”,”BMW”);
// condensed array
// condensed array
3. var myCars=[“Honda”,”Suzuki”,”BMW”];
// literal array
// literal array
Demonstrate the use of array in javascript
<!DOCTYPE HTML> <html> <head> <title> JavaScript Language </title> <script type="text/javascript"> var i; var mycars = new Array(); mycars[0] = "Honda"; mycars[1] = "Suzuki"; mycars[2] = "BMW"; for( i=0 ; i < mycars.length ; i++) { document.write(mycars[ i ] + "<br/>"); } </script> </head> </html>
Comments
Post a Comment