Arrays

From CodeStuff
Jump to: navigation, search

Arrays are a good way to handle lists of things. In JavaScript, Arrays use the square brackets [ and ]

To create an Array, all you need to do is [] for an empty array or[9,3,2] for an array with three numbers. You can assign these to variables like any other value.

   var mt = [];
   var m = [9,3,2];

To access individual items of the list you use the square brackets again.

   var p = m[1]; 

Entries in the list start counting from zero so m[0] is the first item in the list. [2] accesses the item at position 2 but it is important to remember that position 2 is the third item in the list. If you are new to programming, this might seem a bit odd at first. don't worry, it doesn't take too long to adjust to counting from zero.


You can also add items to lists. The push function is available to add items to the


The mozilla foundation has documentation on Javascript Arrays at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

In additon to the standard features of Arrays available to JaveaScript users, Code on this wiki has been sweetened with SugarJS The additional features are documented at http://sugarjs.com/api/Array

SugarJS allows some cool abilities like exclude