Logical Operators: AND, OR and NOT
A permission can depend on more than one answer
A school trip requires both a signed form and an available seat. Either a parent or a guardian may sign, and a blocked account should not be admitted. These rules combine separate yes-or-no answers.
AND requires both conditions, OR requires at least one, and NOT reverses a boolean answer. JavaScript uses &&, || and !.
Translate a rule
Choose AND for joint requirements and OR for accepted alternatives.
Trace evaluation
Explain why the second condition may not run.
Handle operands
Distinguish boolean results from the values returned by JavaScript && and ||.
Both trip requirements must hold
Signed form
The consent requirement is satisfied.
trueSeat available
No seat remains.
falseCombine with AND
One failed requirement prevents qualification.
true && false → false
OR means at least one, not exactly one
With boolean operands, AND is true only when both are true. OR is true when one or both operands are true. NOT changes true to false and false to true. A rule requiring exactly one alternative needs a different expression.
Group conditions to show the intended policy. “A parent or guardian signed, and a seat is free” becomes a grouped OR followed by AND. Read each component before reading the combined expression.
Before starting, review comparisons that produce booleans. These examples use JavaScript; other languages can use different syntax and rules.
Read the permission rule in pieces
The signed parent or guardian condition is true because the guardian signed. The seat condition is false, so the combined AND is false.
NOT blocked is true because blocked is false. It answers a separate question; it does not override the missing seat.
JavaScript · local teaching example
const parentSigned = false;
const guardianSigned = true;
const seatAvailable = false;
const blocked = false;
console.log((parentSigned || guardianSigned) && seatAvailable);
console.log(!blocked);Expected output
false
trueSome conditions never need to run
JavaScript evaluates these expressions from left to right. With false on the left of AND, the whole condition cannot become true, so the function on the right is not called.
The function below prints when it runs. After the first expression, only false appears. In the second, true on the left means the right condition is needed, so “Seat checked” appears before the final true. This is called short-circuit evaluation.
JavaScript · local teaching example
function checkSeat() {
console.log('Seat checked');
return true;
}
console.log(false && checkSeat());
console.log(true && checkSeat());Expected output
false
Seat checked
trueJavaScript can return an operand instead of a boolean
JavaScript || returns its first truthy operand, or its final operand if none is truthy. && returns its first falsy operand, or its final operand if all are truthy. These results are not automatically booleans.
Here a blank name uses the fallback text. Zero also falls back under ||, which can be wrong for a valid zero mark. Nullish coalescing ?? falls back only for null or undefined, preserving zero.
JavaScript · local teaching example
console.log('' || 'Unnamed student');
console.log(0 || 10);
console.log(0 ?? 10);Expected output
Unnamed student
10
0Apply the rule to a small change
A guardian has signed and a seat is available, but the student is blocked. Write the complete eligibility rule.
Worked answer
Require a signed parent or guardian, an available seat and NOT blocked. The first two requirements pass, but the blocked condition prevents qualification.
JavaScript · local teaching example
const parentSigned = false;
const guardianSigned = true;
const seatAvailable = true;
const blocked = true;
console.log((parentSigned || guardianSigned) && seatAvailable && !blocked);Expected output
falseRecognize mistakes and limits
A right-hand side skipped by short-circuiting does not perform its side effects. Avoid placing essential saving or logging only there unless conditional execution is intended.
Bitwise & and | are different from logical && and ||. Also distinguish a fallback for missing data from a fallback for every falsy value, especially zero and false.
For the language or tool rules used here, see JavaScript operator rules.
Check the core idea
Apply the idea to a new situation
JavaScript · code for this question
function checkSeat() {
console.log('Seat checked');
return true;
}
console.log(false && checkSeat());
console.log(true && checkSeat());Combine requirements without hiding their meaning
Use AND for joint requirements, OR for allowed alternatives and NOT to reverse a condition. Group the intended policy and remember that short-circuiting can skip work. JavaScript operand-return rules need care outside boolean inputs.
Connect this idea with choosing a branch and truthiness and fallback values.
Reaching the recap and completing the quizzes are separate milestones.