Bitpath

Lesson 1 · Unit 02

Conditions

Comparisons, if and else, and combining questions

13 exercisesabout 3 min

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?

TypeScript
let age = 20;
console.log(age > 18);

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.

TypeScript
let n = 7;
console.log(n === 7); // true
console.log(n !== 7); // false
console.log(n >= 8);  // false

Concept

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.

TypeScript
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.

TypeScript
let age = 20;
let ticket = false;
console.log(age >= 18 && ticket); // false
console.log(age >= 18 || ticket); // true
console.log(!ticket);             // true

9 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.

One email when Bitpath is ready to try. Nothing else. Privacy