typescript · level 1 · unit 2 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A ="button" class="pl-term" data-term="0" aria-expanded="false">variable is a box with a name and a label.
A variable is a box with a name on it. let name: string = "Sam"; makes a box called name and puts the "Sam" inside. From then on, say the name to get it back.
The : string part is a type label. A type is the kind of value a box may hold: string for text, number for numbers, and for true or false.
Put the wrong kind of value in a box and the checker stops you before anything runs. Think of a jar labelled jam: no pickles allowed.
let name: string = "Sam";
let age: number = 9;
let hungry: boolean = true;
console.log(name, age, hungry); Read the = as “put into”, not “equals”.
guess first
Quotes or no quotes? What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let pet: string = "Rex";
console.log("pet");
console.log(pet);
your turn
Your turn. Fill your own boxes.
Type your name between the quotes and your age as a number, with no quotes round it. Then press run.
fix it
Game over too soon. Can you fix the lives?
- Read the error:
TS2322: Type 'string' is not assignable to type 'number'.Assignable means allowed to go in. - The label says
number, but "three" is in quotes, so it is a string. It does not fit the box. - Change "three" to 3, with no quotes, then press run.
nailed it
you made the computer remember. :D
- A variable is a box with a name on it.
- The type label says what kind of value fits.
TS2322means the value does not match the label.
new words in your collection
- variable
- value
- type
- boolean
This lesson isn’t saved. A free account remembers every lesson you finish.