arrays are declared using square brackets a :
var name_of_array = [ ];
name_of_array [0] = "Nadal" ;
name_of_array [1] = "Federer" ;
If the array has two elements they are indexed as 0 and 1 within the [ ] . We always start with 0 and end at n-1 where n is the size of the array .
Arrays can also be declared as :
var string_array = ["Andy", "Federer", "Nadal"];
Different data types can be combined in an array :
var array_name = [ 10 , "Andy" , 34 ] ;
Array of an array
There can be an array whose elements himself are arrays e.g.
var array1 = [ "Federer" , "Nadal" , "andy " ] ;
var array2 = [ "Sachin " , "Ponting" , "Lara " ] ;
var array3 = [ "10 " , "Lancer" , "30 " ] ;
var array_of_arrays = [array1,array2,array3];
var x = array_of_arrays[1,2];
array_of_arrays[1,2] refers to 3rd element of array2 . Such arrays are also known as two dimensional arrays .
Sometimes you need to know the length of the array . This is done as :
var x = array1.length ;
This will store the length of array1 in variable x .
Associative Arrays
The indices inside the square bracket are not numbers but strings . These strings are more suitable to real life problems as it combines strings to strings or strings to numbers or other arrays etc. e.g.
var telephone_num = [ ];
telephone_num["Andy"] = 9822334455;
telephone_num["Federer"] = 9765443322;
var name_of_array = [ ];
name_of_array [0] = "Nadal" ;
name_of_array [1] = "Federer" ;
If the array has two elements they are indexed as 0 and 1 within the [ ] . We always start with 0 and end at n-1 where n is the size of the array .
Arrays can also be declared as :
var string_array = ["Andy", "Federer", "Nadal"];
Different data types can be combined in an array :
var array_name = [ 10 , "Andy" , 34 ] ;
Array of an array
There can be an array whose elements himself are arrays e.g.
var array1 = [ "Federer" , "Nadal" , "andy " ] ;
var array2 = [ "Sachin " , "Ponting" , "Lara " ] ;
var array3 = [ "10 " , "Lancer" , "30 " ] ;
var array_of_arrays = [array1,array2,array3];
var x = array_of_arrays[1,2];
array_of_arrays[1,2] refers to 3rd element of array2 . Such arrays are also known as two dimensional arrays .
Sometimes you need to know the length of the array . This is done as :
var x = array1.length ;
This will store the length of array1 in variable x .
Associative Arrays
The indices inside the square bracket are not numbers but strings . These strings are more suitable to real life problems as it combines strings to strings or strings to numbers or other arrays etc. e.g.
var telephone_num = [ ];
telephone_num["Andy"] = 9822334455;
telephone_num["Federer"] = 9765443322;