Array is a special kind of variable that can hold multiple values. If you recall the lesson on JS variables, we explained that variable is a container of a value.
Lets use analogy of a box. A simple variable is a box that holds one item like box containing a large chocolate cake. Now imagine array as a box that has many subdivisions and each compartment has a pastry of a different flavor.
Box compartment 1 holds value = strawberry flavor
Compartment 2 value= Vanilla and so on
Practically Array is many variables within one.
Why do we need Javascript Arrays / Advantages of using Arrays
When we have to loop through many variables, it is much simpler to reference sub container id (usually called index) numbers instead of individual variable names. For example- looping through marks of students to check the highest score. Either write individual variable names OR array name 'student' and id 1 to 50. easier to loop through digits 1 to 50 than text names.
All the data in the array is of same type like text, numerical etc. so it is easier to work with.
Syntax Define / Create Array- Method 1
First create an array, then assign values to its individual indexes.
var arrayname = new Array ();
arrayname[0] = "value";
arrayname[1] = "value";
arrayname[2] = "value";
arrayname[3] = "value"; And so on
Notice that the subcontainer IDs start from 0
Example
js array
Notice how we referred to the 3rd index the array using the document.write command to display it on browser screen.
Syntax Create Array- Method 2
Create an array and assign value in the same statement / command
var arrayname = new Array("value", "value", "value");
Example
javascript define array example
Both Methods are valid. Use the one you find easier and faster.
Lets use analogy of a box. A simple variable is a box that holds one item like box containing a large chocolate cake. Now imagine array as a box that has many subdivisions and each compartment has a pastry of a different flavor.
Box compartment 1 holds value = strawberry flavor
Compartment 2 value= Vanilla and so on
Practically Array is many variables within one.
Why do we need Javascript Arrays / Advantages of using Arrays
When we have to loop through many variables, it is much simpler to reference sub container id (usually called index) numbers instead of individual variable names. For example- looping through marks of students to check the highest score. Either write individual variable names OR array name 'student' and id 1 to 50. easier to loop through digits 1 to 50 than text names.
All the data in the array is of same type like text, numerical etc. so it is easier to work with.
Syntax Define / Create Array- Method 1
First create an array, then assign values to its individual indexes.
var arrayname = new Array ();
arrayname[0] = "value";
arrayname[1] = "value";
arrayname[2] = "value";
arrayname[3] = "value"; And so on
Notice that the subcontainer IDs start from 0
Example
js array
Notice how we referred to the 3rd index the array using the document.write command to display it on browser screen.
Syntax Create Array- Method 2
Create an array and assign value in the same statement / command
var arrayname = new Array("value", "value", "value");
Example
javascript define array example
Both Methods are valid. Use the one you find easier and faster.
Aucun commentaire:
Enregistrer un commentaire