CHAPTER - 12
ARRAY IN JAVASCRIPT
An array is a variable that can store many variables within it. Many programmers have seen arrays in other languages, and they aren't that different in JavaScript.
The following points should always be remembered when using arrays in JavaScript:
1. The array is a special type of variable.2. Values are stored in an array by using the array name and by stating the location in the array you wish to store the value in brackets. Example: myArray[2] = "Hello World";
3. Values in an array are accessed by the array name and location of the value. Example: myArray[2];
4. JavaScript has built-in functions for arrays, so check out these built-in array functions before writing the code yourself!
Example 1 :
<script type="text/javascript">
var arr = new Array(); // constructor Arrayarr[0] = "Football";
arr[1] = "Baseball";
arr[2] = "Cricket";
document.write(arr[0] + arr[1] + arr[2]);
//We can access the value using the for loop also
for (i=0; i<arr.length;i++){
document.write(arr[i]+’<br>’);
}
</script>
Example 2 :
<script>var days = new Array(‘Sunday’,’ Monday’,’ Tuesday’);
OR
var days = [‘Sunday’,’ Monday’,’ Tuesday’];
</script>
Comments
Post a Comment