typescript · level 1 · unit 6 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An keeps many values together in one box.
An array is like a shopping list: many values kept in order, in one box. Write the items inside square brackets, [ and ], with a comma between each.
The type label is the type followed by empty square brackets. string[] means an array of strings, and number[] an array of numbers. Only that kind of item may go in.
.length tells you how many items the array holds. Show the whole array with console.log and you see every item inside square brackets.
let shopping: string[] = ["bread", "milk", "eggs"];
let prices: number[] = [1.2, 0.9, 2.5];
console.log(shopping);
console.log(prices.length); When you show an array, the items appear with no spaces after the commas.
guess first
Four pets. What does .length say?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let pets: string[] = ["cat", "dog", "hamster", "fish"];
console.log(pets.length);
your turn
Your turn. Pack your bag.
Type three things you would pack for a day out, one between each pair of quotes. Then press run.
fix it
One score sneaked in as text. Can you fix it?
- Read the error:
TS2322: Type 'string' is not assignable to type 'number'.Which item is it about? - The label
number[]means every item must be a number. One of them is in quotes. - Remove the quotes round 9, then press run.
nailed it
your first array is ready. :D
- An array keeps many items in order, in square brackets.
string[]andnumber[]say what kind of items fit..lengthtells you how many items there are.
new words in your collection
- array
- item
This lesson isn’t saved. A free account remembers every lesson you finish.