Single-Dimensional Arrays
Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored in a row starting from 0 to the size of array - 1 (array-1 is because array starts from 0).
for eg:
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
Also can be written as :
var mycars = ["Saab","Volvo","BMW"];
The above code declares and initializes an array of 3 string items, like this we can create 'N' number of items.
To Read the Array
We use for loop to read the array,
for (var i=0 ; i< mycars.length ; i++)
{
document.write (mycars[i]);
}
Explanation :
In For loop there are 3 statements
Statement 1 : var i=0
Statement 2 : i< mycars.length
Statement 3 : i++
Statement 1 is executed before the loop (the code block) starts.
Statement 2 defines the condition for running the loop (upto array length).
Statement 3 is executed each time after the loop (the code block) has been executed.
and print the each time the value.
Output will be :
Saab
Volvo
BMW
Try It Your Self : http://www.w3schools.com/js/tryit.asp?filename=tryjs_array
Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored in a row starting from 0 to the size of array - 1 (array-1 is because array starts from 0).
for eg:
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
Also can be written as :
var mycars = ["Saab","Volvo","BMW"];
The above code declares and initializes an array of 3 string items, like this we can create 'N' number of items.
To Read the Array
We use for loop to read the array,
for (var i=0 ; i< mycars.length ; i++)
{
document.write (mycars[i]);
}
Explanation :
In For loop there are 3 statements
Statement 1 : var i=0
Statement 2 : i< mycars.length
Statement 3 : i++
Statement 1 is executed before the loop (the code block) starts.
Statement 2 defines the condition for running the loop (upto array length).
Statement 3 is executed each time after the loop (the code block) has been executed.
and print the each time the value.
Output will be :
Saab
Volvo
BMW
Try It Your Self : http://www.w3schools.com/js/tryit.asp?filename=tryjs_array
No comments:
Post a Comment