|
Adding/Updating Array Elements
Using the index, you can also update elements of the array. The next example updates the third element (index 2) and prints the contents of the new array.
>>> a[2] = 'three';
"three"
>>> a
[1, 2, "three"]
You can add more elements, by addressing an index that didn't exist before.
>>> var a = [1,2,3];
>>> a[6] = 'new';
"new"
>>> a
[1, 2, 3, undefined, undefined, undefined, "new"]
If you add a new element, but leave a gap in the array, those elements in between are all assigned the undefined value. Check out this example:
>>> var a = [1,2,3];
>>> a[6] = 'new';
"new"
>>> a
[1, 2, 3, undefined, undefined, undefined, "new"] |
|