Thursday, March 19, 2015

JavaScript: Arrays














Once you've graduated from the basic variables in JavaScript the next variable you want to learn is the JavaScript array.  An array is a group of variables in a list or collection.  However, you want to word it.  As with everything else a JavaScript array is mutable, meaning you can change it dynamically while you are working on it.

Declaring an Array
var alphabet = new Array();

alphabet [0] = 'A';
alphabet [1] = 'B';
alphabet [2] = 'C';

Declaring an Array In Literal Format
 
var alphabet = ['A','B','C']

Accessing Array Value
 
document.writeln("First Index: " + alphabet[0]);

Output:
First Index: A

Looping through an Array
 
for(var i = 0; i < alphabet.length;i++)
{
document.writeln("index: " + i + " value: " + alphabet[i]);
}

Output:
index: 0 value: A index: 1 value: B index: 2 value: C

No comments:

Post a Comment