Introduction to Javascript - Getting to know Arrays

Guest Posts Paolo Nikko Nuñal

Arrays are important most especially if you are dealing with lots of numbers. In this tutorial you are going to deal with arrays in Javascript and how you can manipulate it with your program.

So What is an Array?

An array is a special variable that can hold more than one value at a time. Think of it as a big packagewith many compartments or small boxes in it. Each compartment has a different address that you can store a value in it.

Not using an Array


var box1="toys";
var box2="cars";
var box3="cellphones";

That is what most people do if they are dealing with variables and storing them individually. What if you happen to have 1000 boxes? You really can't assign 1000 variables, but rather you use an array to store them easily.


var box[0]="toys";
var box[1]="cars";
var box[2]="cellphones";

Based from the above example, box[0] contains "toys", box[1] contains "cars" and box[2] contains "cellphones". An array can hold many values under a single variable name and you can access each value through their index number.

Creating an Array

This how you can create an array.


var myArray = new Array();

From that example, we are storing myArray() with three values. To access a single value from an array you an do this.

var toys = myArray[0];

The statement above stores whatever inside the address myArray[0]. In this case we store the string "Lego" to the variable "toys".

Arrays are flexible in Javascript and you can store different objects in one array. You can store objects, functions, variables in one array.



There you have it, the different things you can do inside an array and how you can access it.

For more tutorials you can check out the courses from Dr. Richard Stibbard.

Learn HTML5/PHP/AJAX Web Programming from Scratch – An amazing course from Dr. Richard Stibbard. Usually I only promote my own courses, but this course is very impressive, so I cut a deal with Dr. Stibbard … You can also find Richard’s website here