typescript · level 1 · unit 3 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Quotes turn a number into text.
12 is a number. "12" in quotes is a string that happens to contain digits. They look alike, but TypeScript treats them very differently.
With numbers, + adds: 5 + 3 is 8. With strings, + joins: "5" + "3" is "53". Mix one of each and it still joins, so "5" + 3 is "53" too.
Not sure which you have? tells you the type of a value, as a string like "number". And the checker will never let a string slip into a number box.
console.log(typeof "12");
console.log(typeof 12);
console.log(12 + 12);
console.log("12" + "12"); A phone number is a string, because you never do maths with it.
guess first
One string, one number. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
console.log("5" + 3);
your turn
Your turn. Count the fruit bowl.
Type how many apples and how many pears are in the bowl, as numbers with no quotes. Guess the total, then press run.
fix it
Four cookies and two more. Fix the count!
- Read the error. Line 3 tries to put a string into a number box. Where does the string come from?
- cookies holds "4" in quotes, a string. A string plus a number joins them into "42", not 6.
- Make cookies a number: change line 1 to let cookies: number = 4; then press run.
nailed it
you can tell text and numbers apart. :D
- Quotes make a string, even if it holds digits.
+adds numbers but joins strings.typeoftells you the type of any value.
new words in your collection
- typeof
This lesson isn’t saved. A free account remembers every lesson you finish.