javascript · 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.
Write a string between backticks, the slanted quote marks, and you get a template literal. Anything inside ${} is swapped for its value, like filling in the gaps on a birthday card.
Remember, prompt always gives back text. To do maths with it, it first. Number("12") turns the text "12" into the number 12.
You can wrap one inside the other. Number(prompt("How old are you?")) asks the question and converts the answer in one go.
let name = "Sam";
let age = 9;
console.log(`Happy birthday, ${name}! You are ${age}.`);
console.log(`Next year you will be ${age + 1}.`);
console.log(Number("12") + 1); On most UK keyboards, the backtick key sits to the left of the 1 key.
guess first
Normal quotes this time. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let pet = "Rex";
console.log("Good dog, ${pet}!");
your turn
Your turn. Look ten years ahead.
Type a name between the quotes and an age as a number, with no quotes. Then press run to see the gaps filled in.
fix it
The answer typed in is 9. Fix the birthday maths!
- It says 91, not 10. What kind of value does
promptalways hand back? - The answer is the string "9", so
+joins instead of adding. Convert it to a number first. - Wrap the prompt in Number( ) on line 1, so it reads Number(prompt("How old are you?")), then press run.
nailed it
you dropped values into your text. :D
- Backticks make a template literal, with
${}for each gap. promptgives back text, even for numbers.Number()converts text into a number you can add.
new words in your collection
- template literal
- convert
This lesson isn’t saved. A free account remembers every lesson you finish.