Sunday, August 31, 2014

Arrays

>>> var a = [];
>>> typeof a;
"object"

>>> var a = [1,2,3];

Adding/Updating Array Elements

>>> a[2] = 'three';
"three"
>>> a
[1, 2, "three"]

>>> var a = [1,2,3];
>>> a[6] = 'new';
"new"
>>> a
[1, 2, 3, undefined, undefined, undefined, "new"]

Deleting Elements

>>> var a = [1, 2, 3];
>>> delete a[1];
true
>>> a
[1, undefined, 3]

Arrays of arrays

>>> var a = [1, "two", false, null, undefined];
>>> a
[1, "two", false, null, undefined]
>>> a[5] = [1,2,3]
[1, 2, 3]
>>> a
[1, "two", false, null, undefined, [1, 2, 3]]

>>> var a = [[1,2,3],[4,5,6]];
>>> a
[[1, 2, 3], [4, 5, 6]]

>>> a[0]
[1, 2, 3]

>>> a[0][0]
1

>>> var s = 'one';
>>> s[0]
"o"

No comments:

Post a Comment