Lesson 1 · Unit 02
Conditions
Comparisons, if and else, and combining questions
What you practise
===and!==,&&,||,!,if,else if,else, the ternary, comparing values of different types is a compile error
Try a question from this lesson
Pick one
> asks a question: is the left side bigger than the right? What do you think this prints?
let age = 20;
console.log(age > 18);Correct answer
true
Why
20 is bigger than 18, so the answer to the question is yes, and yes is written true. A comparison always works out to a boolean: true or false. That is the third type you met, next to number and string.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Comparisons
Six operators compare two values. Each gives a boolean.
> bigger, < smaller, >= bigger or equal, <= smaller or equal.
=== asks "are these equal?" and !== asks "are these different?".
Careful: one = stores a value, three compare. n = 7 changes n. n === 7 only asks.
let n = 7;
console.log(n === 7); // true
console.log(n !== 7); // false
console.log(n >= 8); // falseConcept
if, else if, else
An if can be followed by more questions with else if, and by a final else.
The conditions are checked from the top. The first one that is true runs its block, and all the rest are skipped, even if they are true as well.
else has no condition: it catches everything that is left.
let score = 72;
if (score >= 90) {
console.log("A");
} else if (score >= 70) {
console.log("B");
} else {
console.log("C");
}Concept
And, or, not
Booleans can be combined.
a && b (and) is true only when both sides are true.
a || b (or) is true when at least one side is true.
!a (not) flips a boolean: true becomes false and false becomes true.
let age = 20;
let ticket = false;
console.log(age >= 18 && ticket); // false
console.log(age >= 18 || ticket); // true
console.log(!ticket); // true9 more exercises in the app
- 1× true or false
- 3× multiple choice
- 3× type the answer
- 1× fill the blanks
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.