&& (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.
&& and || return one of their operands, not necessarily a boolean: a || b returns a if a is truthy, otherwise b; a && b returns a if a is falsy, otherwise b. This is why name || "Guest" works as a default.
Short-circuiting enables guards such as user && user.name, though optional chaining (user?.name) is now clearer. ?? differs from || by only treating null and undefined as missing; mixing ?? with &&/|| requires parentheses.
De Morgan's laws help simplify conditions: !(a && b) equals !a || !b, and !(a || b) equals !a && !b.
Logical assignment operators (||=, &&=, ??=) assign only when the short-circuit condition allows it.
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.
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.
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);
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
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).
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 explanationLogical 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 predictionShort-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.
DebuggingRule 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 generationRules 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 reviewCondition 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.