Every interesting program has to ask questions and act on the answers. Is the
player's score high enough for a new level? Is it raining and a school day? Are you
not logged in? Behind each of these is a tiny yes/no question, and the answer is always one
of just two values: true or false. A value that can only be true
or false is called a
This page is about the two families of operators that build and combine those yes/no answers:
> or === — compare two
values and hand back a boolean: they make a true/false answer.&&),
OR (||) and NOT (!) — take booleans and
combine them into a single true/false answer.
Together they let you write conditions like "tall enough and old enough" — the exact rules
that
A comparison looks at two values and asks a single yes/no question about them. There are six you will use constantly, and each one gives back a boolean:
a === b — is a !== b — is a > b — is a < b — is a >= b — is a <= b — is
The answer isn't just used to make a decision — it is a real value you can store or print. Press
Run and read the booleans a set of comparisons produces, then change
score and Run it again:
Notice === uses three equals signs. One equals (=)
assigns a value into a variable; three equals compares two values and gives back
true or false. Getting those two mixed up is the classic beginner slip — more on that at the end.
One comparison is often not enough. A rollercoaster might need you to be tall enough and old enough; a film ticket might be free if you are a member or under 5. The three logical operators glue booleans together — or flip one over:
&&) — true only when both sides are true. Think
"I need this and that."||) — true when at least one side is true. Think
"this or that will do."!) — flips a single boolean: !true is
false, and !false is true.
The || symbol is two vertical "pipe" bars (usually the key just above Enter or
next to left Shift). Here is the rollercoaster rule in code — you can ride if you are tall
enough AND old enough. Try changing the numbers so one rule fails:
Swap the && for || and the rule loosens to "tall enough or
old enough" — now passing either test is enough. Choosing between AND and OR is choosing
exactly what your program means.
The whole behaviour of each logical operator fits in a small truth table: list every possible combination of the inputs, and write down the result. Because a boolean is either true (T) or false (F), there aren't many rows to fill in. Green cells are true, red are false:
Read AND across: the only true row is the one where both inputs are true. Read OR across: the only false row is where both are false — every other row is true. And NOT simply mirrors its input: it turns T into F and F into T. Memorise these three tiny tables and you understand logical operators completely.
Imagine a cinema rule: to watch a 15-rated film on your own you must be 15 or older. If you're younger, you can still go — but only if you are with an adult. We can capture that whole rule in one condition using OR: you may enter if you are old enough or you have an adult with you.
NOT is handy for phrasing a rule the other way round. "You may enter" is exactly the opposite of "you
are turned away", so turnedAway is just !(oldEnough || withAdult) — the same
rule flipped with a single !.
Yes — and you often will. You can combine as many comparisons as you like with
&& and ||, using round brackets to make the meaning crystal clear,
just like in maths:
Here age >= 8 && age <= 12 checks the age is between 8 and 12,
and the outer && hasTicket then insists on a ticket as well. When a condition
gets long, brackets are your friend — they show exactly which parts group together.
The most common logic bug is muddling up && and ||. They look
similar but demand completely different things:
&& (AND) needs BOTH sides true. It is fussy — the
more &&s you add, the harder the condition is to satisfy.|| (OR) needs EITHER side true. It is generous — the more
||s you add, the easier the condition is to satisfy.
So "you must be a member && over 18" lets in far fewer people than
"you must be a member || over 18". If your program is letting in too many people, you
probably wrote || where you meant && (or the other way round).
And remember ! (NOT) flips a boolean — it doesn't "remove" it.
!isRaining is true exactly when it is not raining. A double flip cancels out:
!!isRaining is just isRaining again.