Why do we use arrays in JavaScript? Well we should know that we have variable in javaScript in which we save values for those variables. Like we have a variable with name var myname in which we save my name. What if we have multiple values of same category to save will we create variable for each of that value? Let’s say we have to save whole alphabets in a variable what will we do ? Will we save them like var alphabet_1 = a; var alphabet_2 = b; ? This would become a very difficult way to save them.
We use arrays to save multiple values of same category. An array can hold unlimited values in it. Suppose if you create an array with name of fruits and you can save so many fruits in this array rather to create new variable for each fruit. In JavasCript we start array with var as we do for string but javaScript understand this is an array cause of comma separators after each array element. In JavaScript we separate each array element with comma. We use quotes for strings and we put without quote for numbers.
There are two methods to create array in JavaScript one is var arrayname = new array(“element_1”, ‘element_2’); And the other method to declare an array in JavaScript is var myarray = [“element_1”, “element_2”]; Its upto you which method you want to use and which you feel is easy to handle.
Example of Array in JavaScript
[code lang=”js”]
<script type="text/javascript">
var myName = "Ateeq"; //This is an example of A variable.
var alphabets = new array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"); //This is example of Array of alphabets from a to J.
var days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; //This is another method to create array in JavaScript
</script>
[/code]
Calling Array elements In JavaScript.
When we have declared an array or we have created an array now we need to use the array how can we can that array? Well when we created array javaScript made its indexes for each element javaScript start indexing from 0 to so on means if your array have elements a, b, c , d they will be indexed a = 0, b = 1, c = 3 so on. If you want to print one value then you can use its index if you want to print all values of array then you can use loop as well. Loop will print all values for you with few lines of codes instead of calling each element one by one.
[code lang=”js”]
<script type="text/javascript">
var myName = "Ateeq"; //This is an example of A variable.
var alphabets = new array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"); //This is example of Array of alphabets from a to J.
var days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; //This is another method to create array in JavaScript
//Following line of code will alert box with Mon.
alert(days[1]);
//following alert box will alert c
alert(alphabets[2])
//calling all alphabets and writing them on document with while loop
var counter=0; //creating counter to index array value starts from 0
while (alphabets[counter]) //while loop starts here.
{
document.write(alphabets[counter] + "<br>"); // printing alphabet and putting line break in html
counter++; // ++ in javaScript plus 1 in the number.
} //while loop ends here.
</script>
[/code]
What ++ do in JavaScript?
In JavaScript like other programming languages php, c++ etc ++ do same this adds value 1 to the already existing number. Suppose we have saved 1 in a variable counter if we write counter++ that will make it 2 when this line of code get execute.