Student-friendly HTML, CSS, and JavaScript reference with examples
if (age >= 18 && hasTicket)
You already know &&, || and ! — they take booleans and give back a boolean. An
if wants a boolean. So they go straight into the parentheses.
let age = 25;
let hasTicket = true;
if (age >= 18 && hasTicket) {
letThemIn();
} else {
turnThemAway();
}
&& needs both sides true. Flip hasTicket to false and the train goes right, no
matter how old they are.
let day = "Sunday";
if (day === "Saturday" || day === "Sunday") {
sleepIn();
} else {
setAlarm();
}
|| only needs one side. The first comparison fails, the second passes, so it’s a lie-in.
let isOpen = false;
if (!isOpen) {
showClosedSign();
}
! flips it, so !false is true. Read it out loud: “if it is not open.” Shorter and
clearer than isOpen === false.
if (day === "Saturday" || "Sunday") { // wrong — always true
if (day === "Saturday" || day === "Sunday") { // right
|| needs a full comparison on each side. "Sunday" alone is just a non-empty string,
and that’s always truthy — so the first version runs no matter what day it is.