typescript · level 1 · unit 6 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Every item in an array has an , starting at 0.
Each item has a position number called an index. Counting starts at 0, so queue[0] is the first item and queue[1] the second. The last one is queue[queue.length - 1].
Put a new value at an index to replace that item, like queue[1] = "Dev";. To add an item to the end, it on: queue.push("Eli");.
Ask for an index past the end and you get , which means no value has been put there. The checker cannot spot this one, so count carefully.
let queue: string[] = ["Ava", "Ben", "Cleo"];
console.log(queue[0], queue[queue.length - 1]);
queue[1] = "Dev";
queue.push("Eli");
console.log(queue); The index is always one less than the place in the queue, so the third person is at index 2.
guess first
Which colour is at index 1?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let colours: string[] = ["red", "green", "blue"];
console.log(colours[1]);
your turn
Your turn. Join the queue.
Push your name onto the end of the queue in the first blank. In the second blank, type the index of the last item, then press run.
fix it
Where did the lemon cake go? Fix it!
- There is no error, but it shows undefined. Nothing has been put at that index.
- There are three cakes, but counting starts at 0. So the indexes are 0, 1 and 2.
- Change cakes[3] to cakes[2], then press run.
nailed it
you can pick out any item now. :D
- Indexes start at 0, so the first item is
[0]. .push()adds a new item to the end.- Past the end, you get
undefined, not an error.
new words in your collection
- index
- push
- undefined
This lesson isn’t saved. A free account remembers every lesson you finish.