Arrays

Arrays are similar to variables, but different in that they can hold multiple values and expressions under one name. Storing multiple values in one variable is what makes arrays so powerful. There is no limit to the amount or type of data that you can store in a JavaScript array and, as long as it is within scope, you can access any value of any item in an array at any time after it is declared in your script. While they can hold any data type in the JavaScript language, including other arrays, it is most common to store similar data in any one array and to assign it a name that is related to the items it contains. Example below provides an example using two separate arrays to store similar data.

Storing similar values in an array

var states = new Array("M.P." , "Maharashtra" , "U.P." , "GOA");

var cities = new Array("Indore" , "Mumbai" , "Pune" , "New Delhi");

As you can see, while it may be possible to store all these items in one array, it isn't logical and may cause problems later in the script, such as identifying what data is in the array.

Accessing values in an array is easy, but there is a catch. Arrays always start with an ID of 0, rather than 1, which can be confusing when you're first getting started. The IDs then increment from 0 on up, for example 0, 1, 2, 3, and so on. To access an array item you must use its ID, which refers to the item's position in the array (Below).

Storing similar values in an array

var cities = new Array("Indore" , "Mumbai" , "Pune" , "New Delhi");

document.write("Indore: "+cities[0]);

document.write("Mumbai: "+cities[1]);

document.write("Pune: "+cities[2]);

document.write("New Delhi: "+cities[3]);

It is also possible to assign a value to a position in an array or update an item's value in an array, just as you accessed an item in an array earlier.

Assigning values to specific positions in an array

var cities = new Array();

cities[0] = "Indore";

cities[1] = "Mumbai";

cities[2] = "Pune";

cities[3] = "New Delhi";

document.write("Mumbai: "+cities[1]);

// Update Mumbai to Bangalore

cities[1] = "Bangalore";

document.write("Bangalore: "+cities[1]);

Other Operations :

Loop over an Array

var cities = new Array();

cities[0] = "Indore";

cities[1] = "Mumbai";

cities[2] = "Pune";

cities[3] = "New Delhi";

cities.forEach(function(item, index, array){

document.write(item +" "+ index);

});

// Indore 0

// Mumbai 1

Sort an Array

var cities = new Array();

cities[0] = "Indore";

cities[1] = "Mumbai";

cities[2] = "Pune";

cities[3] = "New Delhi";

cities.sort();

// ["Indore " ,"Mumbai", "New Delhi", "Pune"]

Reverse an Array

var cities = new Array();

cities[0] = "Indore";

cities[1] = "Mumbai";

cities[2] = "Pune";

cities[3] = "New Delhi";

cities.reverse();

// ["New Delhi", "Pune", "Mumbai", "Indore"]

Access (index into) an Array item

var first = cities[0];

// Indore

var last = cities[cities.length - 1];

// New Delhi

Add to the end of an Array

var newLength = cities.push("Bangalore");

// ["Indore" ,"Mumbai" ,"Pune", "New Delhi", "Bangalore"]

Remove from the end of an Array

var last = cities.pop();

// Bangalore will remove

// ["Indore" ,"Mumbai" ,"Pune", "New Delhi"]

Remove from the front of an Array

var first = cities.shift();

// Indore will remove

// ["Mumbai" ,"Pune", "New Delhi"]

Add to the front of an Array

var newLength = cities.unshift("Jaipur");

// Jaipur will add

// ["Jaipur", "Mumbai", "Pune", "New Delhi"]

Remove an item by Index Position

var removedItem = cities.splice(position, 1);

// If position set 0, then Jaipur will remove

// ["Mumbai", "Pune", "New Delhi"]

Copy an Array

var copy = cities.splice();