Getting Started
Level 1
- 4 - Comments
- 5 - Displaying Text
- 6 - Variables - Let
- 7 - Variables - Constants
- 8 - Arithmetic Operators
- 9 - Functions
- 10 - If/Else Statements
- 11 - Comparison Operators
- 12 - If/Else Loops
- 13 - Boolean Operators
- 14 - Level 1 Complete!
Level 2
Level 3
Useful Links
Step 15 - Arrays
An array is an ordered list of values. It can keep any number of values inside. These values can even be of any type — Numbers, Strings, Objects, even other Arrays.
let animals = ['cat', 'dog', 'horse'];
let randomThings = [2, 'book', 'today is Saturday', 10];
let numbers = [1, 2, 8, 19];
Challenge:
Create your own array, named favouriteFood
, and write in a couple of
things you like.
Answer
const favouriteFood = ['lasagna', 'chocolate', 'roast potato']
Array Length
We can easily check how many items we have in our array with a property
by looking at the property .length
.
let randomThings = [2, 'book', 'today is Saturday', 10];
console.log(randomThings.length); // We will get 4 because there are 4 things in the array.
Challenge:
Check how many values you have in your array favouriteFood
and
console.log the result.
Answer
console.log(favouriteFood.length)
Array Usage
It’s useful to keep all of these values in one place. But what if we want to use only 1 item from the array?
We can get them out using ‘bracket notation’. Thanks to a guy named Edsger Dijkstra, array indices start counting from 0. The usage looks like this.
let randomThings = [2, 'book', 'today is Saturday', 10];
let firstItem = randomThings[0];
let secondItem = randomThings[1]; // and so on
Challenge:
Get the third element from your array favouriteFood and console.log it.
Answer
console.log(favouriteFood[2])
Changing Values in Arrays
We also can replace values inside of the arrays by assigning a new value to a specific index.
let animals = ['cat', 'dog', 'horse'];
// let's replace 'dog' with 'fish'
animals[1] = 'fish';
// now our animals array will be ['cat', 'fish', 'horse']
Challenge:
Take your array favouriteFood and replace the first value with anything else. Console.log the whole array to see the change.
Answer
const favouriteFood = ['lasagna', 'chocolate', 'roast potato']
favouriteFood[0] = 'ricotta'
console.log(favouriteFood)
Array.push()
If you want to add new values to an existing array you can use the method
.push()
. Push will add a new value to the end of the array.
let animals = ['cat', 'dog', 'horse'];
animals.push('rabbit');
// animals will be ['cat', 'dog', 'horse','rabbit']
Challenge:
Let’s extend your list of favouriteFood and add one more value to it. Console.log the whole array to see your changes.
Answer
const favouriteFood = ['lasagna', 'chocolate', 'roast potato']
favouriteFood.push('biscuits')
console.log(favouriteFood)
Constant Arrays
An array is what is known as a reference type
. What this means is that
even if an array is declared (created) using ‘const’, the values inside that
array can still be changed; only the array itself cannot be overwritten.
const animals = ['cat', 'dog', 'horse'];
// These are both legal, and works the same way as when animals is a variable:
animals[1] = 'fish';
animals.push('rabbit');
// This is illegal, and will return an error the same as changing any constant:
animals = [ 'mouse', 'elephant' ];
Challenge:
Try creating an array as a constant and modifying the values in it.
Answer
const favouriteFood = ['lasagna', 'chocolate', 'roast potato']
favouriteFood[2] = 'muffins'
console.log(favouriteFood)
Challenge:
See what happens if you add something with .push(), change something with bracket notation (array[1]) and assigning a new whole new array to the constant.
Answer
const favouriteFood = ['lasagna', 'chocolate', 'roast potato']
favouriteFood.push('muffins')
favouriteFood[1] = 'eggs'
favouriteFood = ['cheese', 'bananas', 'spaghetti', 'icecream']
console.log(favouriteFood)