03 - JavaScript Arrays
03 - JavaScript Arrays
Learning Outcomes
1. What is an array?
2. Properties of arrays
3. Declaring an array
4. Array operations
What is an Array?
An array is a data structure that stores an ordered collection of values.
● Each value in the array can be accessed by its index (position) number.
● Index positions always being with 0
● Last item in the array has index n-1, where n is the total number of items in the array
Arrays have a length, ie: the total number of values stored in the array.
Last item is at
index 9
(10-1 = 9)
Declaring an Array
Declaring an array of number values:
If you do not know the position of the last item, you can compute it programmatically:
// this is okay
let x = ["apple", "banana", "carrot", "donut"]
x = [53, 22, 11]
// not okay
const x = ["apple", "banana", "carrot", "donut"]
x = [53, 22, 11] // error!
Updating a value in an array
Update a value by accessing it using its index:
fruits.splice(2,1)
● .shift(): Removes and returns the first item in array (same as .splice(0,1))
● .pop() : Removes and returns last item in array (same as .splice(n,1), where n = position
of last item)