Comparison and logical operators

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

This page is about the two families of operators that build and combine those yes/no answers:

Together they let you write conditions like "tall enough and old enough" — the exact rules that if / else statements branch on.

Comparison: making a true / false answer

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:

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:

const score: number = 7; console.log("score === 10 :", score === 10); console.log("score !== 10 :", score !== 10); console.log("score > 5 :", score > 5); console.log("score < 5 :", score < 5); console.log("score >= 7 :", score >= 7); console.log("score <= 7 :", score <= 7);

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.

Logical operators: combining booleans

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:

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:

const heightCm: number = 145; const age: number = 11; const tallEnough: boolean = heightCm >= 140; const oldEnough: boolean = age >= 12; console.log("tall enough? ", tallEnough); console.log("old enough? ", oldEnough); if (tallEnough && oldEnough) { console.log("🎢 Enjoy the rollercoaster!"); } else { console.log("Sorry — you need to be tall enough AND old enough."); }

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 truth tables

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.

A worked example: can you enter the cinema alone?

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.

function canEnter(age: number, withAdult: boolean): void { const oldEnough: boolean = age >= 15; if (oldEnough || withAdult) { console.log("age " + age + ", adult=" + withAdult + " → welcome in!"); } else { console.log("age " + age + ", adult=" + withAdult + " → sorry, not today."); } } canEnter(16, false); // old enough on their own canEnter(12, true); // too young, but with an adult canEnter(12, false); // too young and alone

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:

const canPlay: boolean = (age >= 8 && age <= 12) && hasTicket;

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:

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.

const isRaining: boolean = false; console.log("isRaining :", isRaining); console.log("!isRaining :", !isRaining); console.log("!!isRaining :", !!isRaining);