Arrays

Arrays are a data type (they're of type Object - told you there would be more later) that let you store a collection of items, or collection of related data. In JavaScript, an array can be of any length and contain any other data type.

To declare an array we use square brackets:

Each element in an array can be accessed by its index, which is it's numerical position in the array. However, unlike in real life, when we're using arrays, we don't start counting at 1 but at 0.

In other words, if I wanted to access the element "pizza"in the favouriteFoods array, I would need to look up the element at index 0 in the array:

Adding elements to an array

If we want to add an element to the end of an array, we can use the .pushfunction:

Updating an array

We can also change the elements in an array by reassigning it (using = the same way as we assign a variable):

results matching ""

    No results matching ""