Adscend

Click Here



Click Here

Monday

Array Tutorial

Arrays

1.Using array, multiple values can be stored under a single name, instead of using separate variables for each item.

2. The Array object is used to store multiple values or more than one value in a similar data type variable.

But in JavaScript, the elements in the array can be of different types. For example in an array you can have both a string and an integer.

3.Array is simply an ordered stack of data items.

4.Items can be added and removed from the array at any time, also their value can be changed easily.

5. Accessing a particular element in an array by referring to the name of the array and the index number.

6. The index number of array starts from 0.

7. If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.

8. Note: Array is a key word.



An array can be defined in three ways:
Numeric array - An array with a numeric ID key
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays

Example: 1
<script type="text/javascript">
var names=new Array("Robert" , "palash", "mike");
alert(names[0]);
</script>


Example: 2
<script type="text/javascript">
var names=new Array();

names[0]="Robert";
names[1]="palash";
names[2]="mike";

alert(names[2]);
</script>


Changing and adding array elements
Example: 3
<script type="text/javascript">
var names=new Array();

names[0]="Robert";
names[1]="palash";
names[2]="mike";
alert(names[0]);

names[0]="sky";
alert(names[0]);
</script>


Here we first insert "Robert" at zero index of array. After we rewrite index zero with "sky".




No comments:

Post a Comment