Lesson 1 · Unit 02
Conditions
Comparisons, if and else, and/or/not, the ternary, and values that count as true
What you practise
===and!==, why not==,&&,||,!,if,else if,else, the ternary, truthy and falsy values
Try a question from this lesson
Pick one
A shop delivers for free on orders of 50 or more. >= means "at least". What do you think this prints?
const order = 62;
if (order >= 50) {
console.log("Free delivery");
}
console.log("Thanks!");Correct answer
Free delivery, then Thanks!
Why
62 is at least 50, so the lines between the curly brackets run. The last line is outside the brackets, so it runs whatever the order is. With an order of 20, only Thanks! would be printed.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Comparisons and if
A comparison gives a boolean: true or false.
> < >= <= compare sizes. === asks "are these equal?" and !== asks "are these different?". A single = stores a value. It never compares.
if (condition) { ... } runs the block in curly brackets only when the condition is true.
console.log(5 > 3); // true
console.log(5 === 3); // false
console.log(5 !== 3); // trueConcept
And, or, not
&& means "and": true only when both sides are true.
|| means "or": true when at least one side is true.
! means "not": it flips true to false and false to true.
const age = 20;
const ticket = false;
console.log(age >= 18 && ticket); // false
console.log(age >= 18 || ticket); // true
console.log(!ticket); // trueConcept
Truthy and falsy
The condition of an if does not have to be a comparison. Any value will do.
false, 0 and the empty string "" count as false. They are called falsy.
Nearly everything else counts as true, or truthy: any other number, and any string with something in it, even "0".
const name = "";
if (name) {
console.log("Hello " + name);
} else {
console.log("No name given");
}
// prints: No name given9 more exercises in the app
- 3× type the answer
- 2× multiple choice
- 1× true or false
- 1× fill the blanks
- 1× match the pairs
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.