Embed presentation
Download as PPSX, PPTX


![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");](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-3-2048.jpg)
![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";](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-4-2048.jpg)
![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>”);](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-5-2048.jpg)
![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;](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-6-2048.jpg)
![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>”);
}
}](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-7-2048.jpg)

Arrays allow you to store multiple values in a single variable. There are multiple ways to create arrays including using array literals or the new keyword. Array elements can be accessed using the array name and index, with indexes starting at 0. Loops like for loops are commonly used to iterate through and display array elements. Two dimensional arrays are arrays whose elements are also arrays, and they use nested for loops to access and display values.


![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");](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-3-2048.jpg)
![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";](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-4-2048.jpg)
![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>”);](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-5-2048.jpg)
![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;](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-6-2048.jpg)
![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>”);
}
}](https://image.slidesharecdn.com/js-arrays-161127052913/75/javascript-arrays-7-2048.jpg)