7. JavaScript: Arrays

In javaScript you can create a variable with keyword var .

var name = "Khan";
var age = 20;
var address = "Stockholm"

If you want to keep all these variables in one place so you can access them one by one. Here comes another powerful data structure called Array. You can add as much data as you want and you can access them one by one.

Declaration of array is same as you declare variable but with one exception [ ] brackets.

var myArray = [ ];

Where ever you will see these [ ] brackets with a variable, it will be an array. In javaScript array is very flexible data structure. You can create the array of integers type, string types and you can create mix type of array which can contain integers and strings both.

Array Declaration

You can declare the array with keyword var, name of the array and [ ].

var myNumbers = []; 
var myStrings = [];
var collections = [];

Assign data to Array

First declare your array and then insert data in it
var myNumbers = [];

Array always accepts data in sequence and that sequence will start from zero.

myNumbers[0] = 111;
myNumbers[1] = 222;
myNumbers[2] = 333;

As you can see that the first value 111 is inserted at position 0 and the second value 222 is inserted at position 1 and so on. Which shows that index of array is 0 based. If you want to retrieve your data from array then you can get it back in same way by using same sequence which starts from 0.

First element of array

var value = myNumbers[0];

Second element of array

var value = myNumbers[1];

and so on, just remember one thing that array is always starts from 0.

javaScript provides another flexible way to declare array. You can declare and initialize your array at the same time.

var myStrings = ["Apple","Orange","Paron"];

Now you have declared an array called myStrings and it contains 3 different fruits name. You can access this array in same fashion.

var fruit = myStrings[0]

variable fruit contains Apple as a value

Types of Array

You can create different types of array in javaScript, integer, string, float and so on. But actually in javaScript, arrays are very very flexible. You can create array with any type of value in it. Check this  very simple example. Declare different types of variables and add them in array.

var myname = "khan"; // String variable
var postcode = 12345; // Integers variable
var today = new Date(); // Date type of object

Declare and initialize array with different types of values

var mixedValueArray = [myname,postcode,today];

add some more data in array.
mixedValueArray[3] = 34.78;

Now mixedValueArray contains not only String, but integer, float and a Date object as well. This is the beauty of javaScript.
You can use for loop to iterate this array. Know more about loops

for(i in mixedValueArray){
   document.write(" " + mixedValueArray[i] + " ")

Associative Array

One more very nice and powerful feature of javaScript is associative array. Which means that just forget about sequences (0,1,2,3) and give some nice names to indexes. Here is how it works

var wishlist = [];

wishlist["car"] = "Ferrari";
wishlist["house"] = "Villa";
wishlist["dog"] = "Basset Hound";
wishlist["machine"] = "Quad Core";

alert("my fav dog is " + wishlist["dog"]);
alert("my fav car is " + wishlist["car"]);

You can see, it’s so convenient that you do not need to remember any index number of your array but you can get any index by just passing the name because you have associated the indexes with names.


Download file from GitHub