Bitpath

Lesson 1 · Unit 02

Conditions

Comparisons, if and else, and/or/not, the ternary, and values that count as true

13 exercisesabout 3 min

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?

JavaScript
const order = 62;
if (order >= 50) {
  console.log("Free delivery");
}
console.log("Thanks!");

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.

JavaScript
console.log(5 > 3);   // true
console.log(5 === 3); // false
console.log(5 !== 3); // true

Concept

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.

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

Concept

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

JavaScript
const name = "";
if (name) {
  console.log("Hello " + name);
} else {
  console.log("No name given");
}
// prints: No name given

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

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