javascript · 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 into text.
JavaScript sorts values into kinds. Text in quotes is a string. A value like 12, with no quotes, is a number. Same digits, different kind, and JavaScript treats them very differently.
Picture a front door with 12 painted on it, and a box of 12 eggs. The door number is a label you read, like "12". The eggs are an amount you can count and add, like 12.
With numbers, + adds, so 5 + 3 is 8. With strings, + joins, so "5" + "3" is "53". Not sure which kind you have? Put in front and it tells you.
console.log(typeof "12");
console.log(typeof 12);
console.log(5 + 3);
console.log("5" + "3"); If + meets a string and a number, it joins them as text.
guess first
Two goals and one goal. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let homeGoals = "2";
let awayGoals = "1";
console.log(homeGoals + awayGoals);
your turn
Your turn. Count the fruit in the bowl.
Type how many apples and how many pears are in the bowl. Use numbers with no quote marks, so the last line says number.
fix it
Four cookies plus two makes 42? Fix it!
- The answer should be 6. Why would JavaScript stick 4 and 2 together instead of adding them?
- One of the boxes holds a string, not a number. Look for quote marks.
- Remove the quotes around 4 on line 1, then press run.
nailed it
you can tell text from numbers. :D
- Text in quotes is a string.
12with no quotes is a number. +adds numbers but joins strings.typeoftells you what kind of value you have.
new words in your collection
- number
- typeof
This lesson isn’t saved. A free account remembers every lesson you finish.