Javascript Arrays

An array is a special variable, which can hold more than one value at a time.
var my_array = [10, "ten", true];
This sets up an array to hold three values all under the single variable name we called my_array. Notice that you can store numbers, strings of text, and Boolean values in an array. Each value needs to be separated by a comma. The values need to go between two square brackets and after an equal sign.
Access the Elements of an Array
You refer to an array element by referring to the index number. This statement accesses the value of the first element in cars:
var name = cars[0];
This statement modifies the first element in cars:
cars[0] = "Opel";
Arrays are a special type of objects. Objects use names to access its "members". In this example, person.firstName returns Peter: Object: var person = {firstName:"Peter", lastName:"Swanson", age:36};
Array Properties and Methods
The real strength of JavaScript arrays are the built-in array properties and methods: Examples var x = cars.length; // The length property returns the number of elements in cars var y = cars.sort(); // The sort() method sort cars in alphabetical order To write the values to a web page: document.write( my_array[0] + "
"); document.write( my_array[1] + "
"); document.write( my_array[2] + "
");
up