The JavaScript array construct is very flexible, and I especially love the fact that you can put anything into a JavaScript array. There is no type police breaking your code. Arrays in JavaScript are not true arrays, but an Object with special array like functionality. So how do we define an Array
var x = new Array (1, 5, "ten", "twenty");
or
var y = [1, 'a', true, 7.5, new Object()];
or
var z = [ [1, "hello"], [2, {name:"test"}]];
Array indexes are zero based and can be accessed as follows
var value = x[0] //value will be 1
var anotherValue = z[1][1]['name'] //anotherValue will be test
Array indexes are generally integers greater than or equal to 0 and less than 232 - 1, anything else would be converted to a string and looked up as a string.
Arrays can also act as a dictionary with ease, using the x variable defined above, you could do the following
x['a'] = 12345
Yip the power of arrays in Javascript is terrifying, but liberating. I will look at some other array functionality in the next instalment
No comments:
Post a Comment