Booleans & Logic

The Course
•
6 min read

Real decisions often involve more than one fact. && means and: both conditions must hold. || means or: at least one must hold. ! means not: it reverses a boolean. Use parentheses to make a combined rule readable even when precedence would allow you to omit them.

For example, a learner can start a session when they have time and their materials are ready. Write down the cases before coding: time/no time crossed with ready/not ready gives four combinations. Checking only the happy case would miss half the possible mistakes.

Two empty-looking values have special names: null means “no value” chosen deliberately, and undefined often means a value was not supplied. You will see them again when working with records.

Conditions can accept other values too. Zero, an empty string, null, undefined, and NaN are falsy, meaning they act like false in a condition; a nonempty string such as "false" is truthy. That makes if (input) a poor substitute for a precise rule. In this course, prefer explicit comparisons and actual booleans. && and || can return non-boolean operands; here we use them only between boolean expressions.

Predict before running

Does having time alone make ready true? Change hasNotebook to true and run again.

Predict, then run JavaScript
const hasTime = true;
const hasNotebook = false;
const ready = hasTime && hasNotebook;

console.log(ready);
console.log(!hasNotebook);
Check your prediction
false
true

Trace each statement in order. If your prediction differed, describe the first place your mental model and the result diverged before trying again.

Short-circuiting protects later work

JavaScript evaluates && from left to right and stops when a condition is false. It evaluates || until one is true. This is called short-circuiting. It lets you check that a value has the right type before using a method that requires that type.

You do not need a function yet to see the benefit. The example checks a number and a string. Calling trim directly on the number would fail, but the type check prevents that call.

Short-circuiting protects later work JavaScript
const first = 42;
const second = "  Loops  ";

console.log(typeof first === "string" && first.trim() !== "");
console.log(typeof second === "string" && second.trim() !== "");
Check your prediction
false
true

The first expression stops after the false type check. The second reaches trim because its input is a string.

Test a rule systematically

Has timeHas energyTime AND energyTime OR energy
falsefalsefalsefalse
falsetruefalsetrue
truefalsefalsetrue
truetruetruetrue

Use this table when the English word “or” is ambiguous. JavaScript’s or includes the case where both conditions are true. ! applies to the expression immediately after it; use !(a && b) when you mean to negate the combined result.

Try it: write a rule for a free afternoon: either Saturday or Sunday, and no appointment. Use a string variable for the day and a boolean for the appointment. Test Sunday with an appointment; that case catches parentheses placed around only half the rule.

Test the edges of a rule

A condition should express a policy, not merely work for one demonstration value. For an allowed range, decide whether each endpoint belongs. A value inside both limits needs AND; a value outside either limit needs OR. Write the two comparisons separately before joining them.

Boolean variables can give a complicated condition a readable name. They also let you inspect a decision before using it in an if statement. Avoid replacing a precise rule with a loose truthiness check: a meaningful zero and an absent value can behave alike in a truthy test.

Inclusive range checks JavaScript
const minutes = 0;
const atLeastZero = minutes >= 0;
const atMostSixty = minutes <= 60;

console.log(atLeastZero);
console.log(atLeastZero && atMostSixty);
console.log(minutes < 0 || minutes > 60);
Check your prediction
true
true
false

The accepted and rejected conditions are complements for these numeric inputs. Zero is allowed because the lower bound uses >=. A positive-number truthy check would not express this same rule.

Group alternative ways to qualify JavaScript
const hasTicket = true;
const isGuest = true;
const venueOpen = false;

console.log(hasTicket || (isGuest && venueOpen));
console.log((hasTicket || isGuest) && venueOpen);
console.log(!(hasTicket || isGuest));
Check your prediction
true
false
false

&& binds more tightly than ||. With an existing ticket, the first expression permits entry even while the venue is closed. The grouped expression requires an open venue for both tickets and invitations. Parentheses make the intended policy explicit.

Try the rule on new inputs

For a 0–60 range, test -1, 0, 1, 60 and 61. Then change the rule to require a ticket or guest invitation AND an open venue. Find an input where leaving out the parentheses permits entry incorrectly.

Your turn

Exercise

Booleans & Logic: practise

A session is allowed when minutes is between 10 and 60 inclusive and the learner is not tired. Print true for 20 minutes and tired = false. Try 9, 60, 61, and tired = true.

Your working space JavaScript
const minutes = 20;
const tired = false;
// Print whether the session is allowed.
Hint 1: the idea

There are three conditions, all required.

Hint 2: a first step

Combine minutes >= 10, minutes <= 60, and !tired using &&.

Show one solution
const minutes = 20;
const tired = false;
const allowed = minutes >= 10 && minutes <= 60 && !tired;

console.log(allowed);
true

Each boundary belongs to the allowed interval. Replacing either && with || would allow inputs that violate another condition; try one change and find a counterexample.

What you should be able to explain

  • Why is the order of the type check and trim important?
  • Does or require exactly one true condition?
  • Translate a weekend-and-no-appointment rule with explicit parentheses.

Was this article helpful?

Your response is saved on this device.