Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial43 Get Data from an Array
In arrays, each data value is sequentially numbered with an index starting at 0, similar to an address. So, if an array has 7 elements, then the first element is at index 0 and the last element is at index 6. Understanding that the count begins with 0 is important for looping through values.
var scale = ["do", "re", "mi", "fa", "so", "la", "ti"];
var d1 = scale[0]; // "do"
var mi = scale[2]; // "mi"
var ti = scale[6]; // "ti"
var d2 = scale[7]; // undefined; out of bounds
In the above example:
Line 3: The value at index 2 is retrieved, even though a person would normally count that as the 3rd value.
Line 5: When there is an attempt to retrieve an error out of the bounds of the array, then JavaScript returns
undefined
, a type of “empty” value.