Arrays
To
declare a variable that contains an empty array, you use square
brackets with nothing between them:
var
a = [];
Console:
typeof
a;
Output:
"object"
*typeof
returns "object"
To
define an array that has three elements, you do this:
var
a = [1,2,3];
When
you simply type the name of the array in the Firebug console, it
prints the
contents of the array:
Console:
a
Output:
[1,
2, 3]
So
what is an array exactly? It's simply a list of values. Instead of
using one variable
to store one value, you can use one array
variable to store any number of values as
elements of the array.
The
elements contained in an array are indexed with consecutive numbers
starting
from zero. The first element has index (or position) 0, the
second has index 1 and so
on.
Here's
the three-element array from the previous example:
Index
Value
0
1
1
2
2
3
In
order to access an array element, you specify the index of that
element inside
square brackets. So a[0] gives you the first element
of the array a , a[1] gives you
the second, and so on.
Console:
a[0]
Output:
1
Console:
a[1]
Output:
2
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.
Console:
a[2]
= 'three';
Output:
"three"
Console:
a
Output:
[1,
2, "three"]
You
can add more elements, by addressing an index that didn't exist
before.
Console:
a[3]
= 'four';
a
Output:
[1,
2, "three", "four"]
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:
Console:
var
a = [1,2,3];
a[6]
= 'new';
a
Output:
[1,
2, 3, undefined, undefined, undefined, "new"]
Deleting Elements
In
order to delete an element, you can use the delete operator. It
doesn't actually
remove
the element, but sets its value to undefined . After the deletion,
the length of
the
array does not change.
Console:
var
a = [1, 2, 3];
delete
a[1];
a
Output:
[1,
undefined, 3]
Arrays of arrays
An
array can contain any type of values, including other arrays.
Console:
var
a = [1, "two", false, null, undefined];
a
Output:
[1,
"two", false, null, undefined]
Console:
a[5]
= [1,2,3]
a
Output:
[1,
"two", false, null, undefined, [1, 2, 3]]
Let's
see an example where you have an array of two elements, each of them
being
an array.
Console:
var
a = [[1,2,3],[4,5,6]];
a
Output:
[[1,
2, 3], [4, 5, 6]]
The
first element of the array is a[0] and it is an array itself.
Console:
a[0]
Output:
[1,
2, 3]
To
access an element in the nested array, you refer to the element index
in another
set of square brackets.
Console:
a[0][0]
Output:
1
Console:
a[1][2]
Output:
6
Note
also that you can use the array notation to access individual
characters inside
a string.
Console:
var
s = 'one';
s[0]
Output:
"o"
Console:
s[1]
Output:
"n"
Console:
s[2]
Output:
"e"
Remembering that:
- An array is a data store
- An array contains indexed elements
- Indexes start from zero and increment by one for each element in the array
- To access array elements we use the index in square brackets
- An array can contain any type of data, including other arrays