Index Numbers

By: Dusty Arlia
Published on September 12, 2012
Last Updated on Saturday, July 11, 2015 at 4:30 PM
Total Updates: 4

Since arrays hold many values you can't simply reference it by name to access one of its elements. A unique number called an index refers to the position of each element in an array. Use the index number to access a particular element. The first position in an array is always referred to as index 0. Here's an example of an array:

var fullNames = ['Dusty Arlia',
                 'Justin Bieber',
               	 'Emma Watson',
                 'Kim Kardashian',
                 'Kanye West',
               	]
alert(fullNames[0]);

In this example, an alert box opens with "Dusty Arlia" written inside it. This shows that arrays are zero-indexed. Zero-indexed means that the first element in an array has an index value of 0. The second element in an array has an index value of 1. To calculate the index value, just subtract one from the element's position number. Here's how you can access each element in the array above:

Element Value Index Number Access Element by Using...
Dusty Arlia 0 fullNames[0]
Justin Bieber 1 fullNames[1]
Emma Watson 2 fullNames[2]
Kim Kardashian 3 fullNames[3]
Kanye West 4 fullNames[4]

Change the value of an array element like this:

fullNames[4] = 'Lady Gaga';

Comments:

Ad:

Ad: