The basics of syntax

INPUT · Slides

Combining conditions

01 / 05

When there is more than one condition

"Pass if the score is 60 or more and attendance is 80% or more." Judgements often need more than one condition.

You could nest one if inside another, but symbols that join conditions together let you write it on one line.

02 / 05

&& means "and"

condition A && condition B is true only when both hold. If either one fails, the whole thing is false.

const age = 15;const height = 150;console.log(age >= 12 && height >= 140);console.log(age >= 12 && height >= 160);

Result

true
false

03 / 05

|| means "or"

condition A || condition B is true if either one holds. It is only false when both fail.

The symbol is two upright bars. Unlike &&, this one is easy to satisfy.

const day = "Sunday";console.log(day === "Saturday" || day === "Sunday");console.log(day === "Monday" || day === "Tuesday");

Result

true
false

04 / 05

! means "not"

Put ! in front and the boolean is flipped over. true becomes false, false becomes true.

To flip a whole expression, wrap it up as !(3 > 5). Without the brackets it is not clear how far the flip reaches.

const raining = false;console.log(!raining);console.log(!(3 > 5));

Result

true
true

05 / 05

Put it in an if

The combined result is a boolean too, so it goes straight into an if.

When you mix && and ||, wrap the part you want judged first in brackets and nobody will misread it. Time to write some.

const score = 82;const attendance = 95;if (score >= 60 && attendance >= 80) {  console.log("credit awarded");} else {  console.log("resit");}

Result

credit awarded