JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 3: Decisions and LogicLesson 12 of 150About 40 minutes

Lesson 12: Logical operators

01Opening question

Free shipping applies to members OR orders over $50, but only AND when the item is in stock. How do programs combine conditions like these?

Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.

02Start with working code

Parentheses make the rule read like the sentence: (member OR over 50) AND in stock.

const isMember = false;
const total = 62;
const inStock = true;
const freeShipping = (isMember || total > 50) && inStock;
console.log(freeShipping);

03Objective and prerequisites

Learning objective: Combine conditions with &&, || and !, predict short-circuit evaluation, and use logical operators to choose values.

You need: Lesson 11: Comparison operators

Skills practised: Logic, Logical operators.

04Explanation

&& (AND) is true only when both sides are true. || (OR) is true when at least one side is true. ! (NOT) flips true and false.

Combine comparisons to express real rules: age >= 18 && hasTicket, isAdmin || isOwner, !isBanned.

&& is evaluated before ||, just as * comes before +. Add parentheses whenever you mix them so the rule is obvious to readers.

JavaScript stops as soon as it knows the answer (short-circuit). With false && anything, the right side never runs; with true || anything, it never runs either.

05Definitions

Logical AND (&&)
True when both conditions are true.
Logical OR (||)
True when at least one condition is true.
Logical NOT (!)
Turns true into false and false into true.
Short-circuit evaluation
Stopping a logical expression as soon as its result is known.
Truth table
A table listing the result of a logical operator for every combination of inputs.

Open the full glossary

06Visual learning object

Logical operator explorer. Choose two values and see what &&, ||, ?? and ! return, and which side was used.

Loading the interactive object…

Open the full Logical operator explorer page with its guided experiment.

07Examples

ex-012-01A truth table Browser-executable JavaScript

Every combination of two booleans with && and ||.

console.log(true && true, true && false, false && false);
console.log(true || false, false || false, false || true);
console.log(!true, !false);
Sample input
None
Expected output
true false false true false true false true
Common mistake
Thinking || means "exactly one". It is true when both are true too.
Try this
Add the line console.log(!(true && false)) and predict it.
Source
examples/ex-012-01-truth-table.js (repository pending publication)

ex-012-02Combining real rules Browser-executable JavaScript

Parentheses make mixed && and || rules match the sentence.

const isMember = false;
const total = 45;
const inStock = true;
console.log((isMember || total >= 50) && inStock);
const isWeekend = true;
console.log(isWeekend && !isMember);
Sample input
None
Expected output
false true
Common mistake
Omitting parentheses: a || b && c is a || (b && c).
Try this
Change total to 55 and predict both lines.
Source
examples/ex-012-02-shipping-rule.js (repository pending publication)

ex-012-03Short-circuit evaluation Browser-executable JavaScript

The right side runs only when needed. Here the log inside the function shows when it is called.

function check(label, value) {
  console.log("checking", label);
  return value;
}
console.log(check("A", false) && check("B", true));
console.log(check("C", true) || check("D", true));
Sample input
None
Expected output
checking A false checking C true
Common mistake
Putting important work on the right side of && and expecting it to always run.
Try this
Swap the two calls on the first line and predict the output.
Source
examples/ex-012-03-short-circuit.js (repository pending publication)

ex-012-04&& and || return a value Browser-executable JavaScript

|| returns the first truthy operand; && returns the first falsy one (or the last).

console.log("" || "Guest");
console.log("Ada" || "Guest");
console.log(0 && "never");
console.log(5 && "five");
console.log(null ?? "default");
Sample input
None
Expected output
Guest Ada 0 five default
Common mistake
Expecting || to always return true or false.
Try this
What does 0 || null || "x" print?
Source
examples/ex-012-04-operands-returned.js (repository pending publication)

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Fix the discount rule

Use parentheses to make a mixed rule correct.

// Rule: students OR seniors get a discount, but only on weekdays.
const isStudent = false;
const isSenior = true;
const isWeekday = false;
const discount = isStudent || isSenior && isWeekday;
console.log("Discount:", discount);

Can the shopper check out?

Combine several conditions with && and !.

const itemCount = 3;
const hasAddress = true;
const paymentDeclined = false;
// TODO: canCheckout is true when there is at least one item,
//       an address is set, and the payment was NOT declined
const canCheckout = false;
console.log("Can check out:", canCheckout);

Library access rules

Translate three sentences into logical expressions.

const age = 15;
const hasCard = true;
const hasParent = false;
const finesOwed = 0;

09Prediction

Commit to an answer first, then run the real code to compare.

const a = true, b = false;
console.log(a && b);
console.log(a || b);
console.log(!a || b);
console.log("" || "none");

What four lines are printed?

10Debugging investigation

Out-of-stock items ship free

Free shipping should apply only to in-stock items for members or orders of $50 or more. A member's out-of-stock order is getting free shipping.

// Rule: (member OR total at least 50) AND in stock
const isMember = true;
const total = 30;
const inStock = false;
const freeShipping = isMember || total >= 50 && inStock;
console.log("Free shipping:", freeShipping);

More cases in the Debugging Detective Laboratory

11Practical scenario

Shipping eligibility

Shipping rules combine membership, totals, stock and destination. Teams write such rules with explicit parentheses and test every combination, because one misplaced || can give free shipping to everyone.

const ok = (true || false) && false;
console.log(ok);

12Knowledge check

1. What does this print?

console.log(true && false);
  • true
  • false

2. What does this print?

console.log(true || true && false);
  • true
  • false

3. When does the right side of a && b NOT run?

  • When a is truthy
  • When a is falsy
  • Always runs
  • Never runs

4. What does this print?

console.log("" || "Guest");
  • true
  • Guest
  • ""
  • false

5. Everyone gets free shipping. Why?

const isMember = true, total = 10, inStock = false;
const free = isMember || total > 50 && inStock;
  • && should be ||
  • Missing parentheses make it isMember || (total > 50 && inStock)... and isMember is true
  • total must be a string
  • inStock is undefined

6. Which is equivalent to !(a || b)?

  • !a || !b
  • !a && !b
  • a && b
  • !a && b

13Solutions and tests

Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.

Automated acceptance tests for this lesson

These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.

  • Fix the discount rule: Prints Discount: false; isStudent is true; The OR is grouped
  • Can the shopper check out?: Prints Can check out: true; Uses && and !
  • Library access rules: Prints true false false; Values are correct; Uses ||, && and !
  • ex-012-01: output matches the documented expected output
  • ex-012-02: output matches the documented expected output
  • ex-012-03: output matches the documented expected output
  • ex-012-04: output matches the documented expected output

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

The lesson lives in the companion repository at curriculum/level-01-foundations/module-03-decisions-and-logic/lesson-012-logical-operators/. Every example on this page is generated from those files.

The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.

15Summary and next steps

  • && needs both, || needs one, ! flips.
  • && binds tighter than ||; use parentheses for mixed rules.
  • Evaluation short-circuits once the result is known.
  • || and && return an operand, which makes || useful for defaults (?? when 0 is valid).

16AI learning prompts

Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.

Concept explanation Logical operators with stories
Context: I am learning &&, || and !.
Task: Explain each with a real-world rule from shopping or travel.
Constraints: Beginner level; include a truth table.
Expected behaviour: I can translate a sentence into a logical expression.
Testing: Give me three sentences to translate.
Code prediction Short-circuit puzzles
Context: I know short-circuit evaluation.
Task: Give me six expressions with console.log calls on both sides and ask what prints.
Constraints: Wait for answers.
Expected behaviour: Each has an exact output.
Testing: Explain the order of evaluation.
Debugging Rule too generous
Context: My discount rule gives discounts on weekends too.
Task: Help me find the grouping problem.
Constraints: Ask me to print each part of the condition.
Expected behaviour: I fix it myself.
Testing: Suggest all combinations to test.
Exercise generation Rules to code
Context: I finished the library rules exercise.
Task: Create two exercises that turn policies into logical expressions.
Constraints: Exact expected output for given inputs.
Expected behaviour: Each uses &&, || and !.
Testing: Include expected output.
Code review Condition readability
Context: I have a long condition with five parts.
Task: Suggest how to split it into named booleans.
Constraints: Keep behaviour identical.
Expected behaviour: Each part has a clear name.
Testing: Provide test cases proving it still behaves the same.