2. Definition
• An array is a special variable, which can hold
more than one value at a time.
• They are a place in memory that can store
multiple values
3. Creating an Array
1. Using an array literal
- Syntax : var array-name = [item1, item2, ...];
- e.g: var cars = [“Toyota", "Volvo", "BMW"];
2. Using the Keyword new:
- e.g: var cars = new Array(" Toyota ", "Volvo",
"BMW");
4. Accessing arrray elements
• We can can access array elements using array
name and element index.
• Arrays indexed by numbers beginning from 0
upward.
• e.g: in the previous example if we want to
change the second element, we use :
cars[1] = “Tucson";
5. Displaying array elements
• To display array elements we go through the
elements using for loop.
• Here we know the number of the elements by
using length attribute of the array. e.g:
cars.length
• e.g:
for(var i=0;i<cars.length;i++)
Document.write(cars[i]+”<br>”);
6. Two dimensional arrays
• They are array which their elements are also
arrays.
• To declare a 2D array:
Var numbers=[[1,2,3],[4,5]];
• To change the value of 4 to 6:
Numbers[1][0]=6;
7. Cont…
• To display the elements we use nested for loop.
e.g:
for(var i=0;i<numbers.length;i++)
{
for(var j=0;j<numbers[i].length;j++)
{
Document.write(numbers[i][j]+”<br>”);
}
}