We have learned about Number, strings and Boolean data types. These are great to store individual amount of data.  Sometimes we need to manipulate a group of data like a set of fruits, a list of ingredients to make a yummy recipe.  We can create as many variables as we can but it would be good to go with array to handle a series or a set of data.

We can declare an array in JavaScript with array markers which are opening and closing square brackets.


var breakFast= [];

The variable breakFast is now an array.

Each segment in an array is called an Element. Every element has an index which is use to reference that element. Index number starts from ‘0’

Let’s assign some values in breakFast array:


breakFast [0] = “BREAD”;

breakFast [1] = “EGGS”;

breakFast [2] = “TEA”;

index-of-array

index-of-array

We can access any element of an array by its index number, in above example if we want to access EGGS we can simple call element with index number 1


alert(breakFast[1]);

We can also assign the values to the array when we are declaring it:


var webDesign =[“HTML”, “CSS”, “JS”];

array-in-js

array-in-js

We can also create an array of arrays which we call a multi-dimensional array.

var morning= [breakFast,webDesign];
multi-dimensional-array

multi-dimensional-array

Lets try to print breakfast Array in alert function


Alert(morning[breakFast]);// it will display BREAD,EGGS,TEA

Alert(morning[0]); // it will also display all the elements of breakfast array

What if we want to just access the second element of the breakFast array from morning array?

multi-dimensional-array

multi-dimensional-array

The above image might help you to understand how we can handle multi-dimensional array.


Alert(morning[0][1]); // it will display EGGS

That’s very much it for this toast. I hope you enjoy it, Fell free to comment your feedback. Take care