Lesson 2 · Unit 01
Variables
Names for values with let and const, three types, and text built from values
What you practise
letandconst, reassignment, numbers, strings and booleans,typeof, template literals, converting withNumber()andString(), what+does to a string and a number
Try a question from this lesson
Pick one
The first line gives the value 4 a name. What do you think the second line prints?
let price = 4;
console.log(price * 3);Correct answer
12
Why
Wherever the name price appears, JavaScript uses the value stored under it, so this is 4 * 3. There are no quotes around price * 3, so it is a calculation, not text.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Variables
A variable is a name for a value. let apples = 5; creates the name apples and stores 5 under it.
From then on you can write the name wherever you would write the value.
Here = does not mean "is equal to". It means: store the value on the right under the name on the left.
let apples = 5;
let pears = 2;
console.log(apples + pears); // 7Concept
let or const?
const is for a name that keeps its value. let is for a name whose value will change.
Use const unless you know the value has to change. Then a reader can see at a glance what stays put.
Give a variable a name that says what it holds. Names made of several words are written likeThis.
const taxRate = 0.2; // stays the same
let itemCount = 0; // will change
itemCount = itemCount + 1;
console.log(itemCount); // 1Concept
Three types
Every value has a type.
A number is for arithmetic: 4, 3.5. A string is text in quotes: "hi". A boolean is one of two values, true or false, written without quotes.
The type decides what you can do with a value, and what + does to it.
console.log(typeof 3.5); // number
console.log(typeof "hi"); // string
console.log(typeof true); // boolean9 more exercises in the app
- 3× type the answer
- 4× multiple choice
- 1× match the pairs
- 1× fill the blanks
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.