typescript · level 1 · unit 3 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A fills in the gaps in your text.
Wrap text in backticks instead of quotes and you get a template literal. The backtick key is often at the top left, under Esc. Inside, ${name} is swapped for the value in the box.
prompt always gives back text, even when someone types digits. To it, use Number(). Number("12") gives the number 12, and String() turns a number back into text.
Text that is not a number, like Number("cake"), gives , short for Not a Number. And Number(null) gives 0, so Number(prompt("Age?")) is safe when nobody answers.
let name: string = "Sam";
let age: number = 9;
console.log(`Happy birthday, ${name}! You are ${age}.`);
console.log(`Next year you will be ${age + 1}.`);
console.log("Candles: " + String(age)); Number() only works on text that looks like a number. Anything else becomes NaN.
guess first
Spot the trap. 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("Good dog, ${pet}!");
your turn
Your turn. Look into the future.
Type your name between the quotes and your age as a number, then press run.
fix it
Dog years need a number. Fix the answer box!
- Read the error. Line 1 tries to put a string into a number box.
promptalways gives back text, even when someone types 11. Convert it first.- Change line 1 to let age: number = Number(prompt("How old are you?")); then press run.
nailed it
your sentences can hold any value. :D
- Backticks and
${}drop values into text. Number()converts text to a number, andString()converts back.- Use
Number(prompt())when you need a number answer.
new words in your collection
- template literal
- convert
- NaN
This lesson isn’t saved. A free account remembers every lesson you finish.