Course content

Module 1

Module 2

Module 3

Module 4

Module 5

Module 6

Module 7

Module 8

Module 9

Module 10

Module 11

Module 12

Module 13

Module 14

Module 15

Module 16

Module 17

Module 18

25 min

Boolean Logic

Combine clear yes/no statements into business decisions

By the end of this lesson

  • Combine clear yes/no statements into business decisions
  • Avoid treating truthiness as validation

Turn a sentence into a decision

A discount requires a loyal customer AND an order above 100 units. Before coding, describe four cases covering loyal/not loyal and above/not above the threshold. Only one combination is eligible.

&& means both conditions must hold. || means at least one holds. ! negates a Boolean. Parentheses help communicate how mixed conditions are grouped.

const completedPurchases = 5;
const subtotal = 120;
const isLoyalCustomer = completedPurchases >= 5;
const exceedsThreshold = subtotal > 100;
const isEligibleForDiscount = isLoyalCustomer && exceedsThreshold;
console.assert(isEligibleForDiscount === true);
console.assert(!(false || false));

Names expose the interpretation: five completed purchases qualify, but exactly 100 does not exceed 100. A long unexplained expression hides those decisions.

Truthiness is a language behavior

Conditions can receive non-Boolean values. 0, "", null, undefined and NaN are falsy; a nonempty string such as "false" is truthy. Empty arrays and objects are also truthy. Therefore testing a raw input's truthiness does not prove that it represents an approved customer or valid number.

&& and || short-circuit: they may skip the right operand. They return operand values, not necessarily Booleans. At this stage, combine explicit comparisons so your eligibility result is actually Boolean.

Guided correction

Replacing AND with OR would approve a loyal customer below the threshold and a new customer above it. Check your four cases against both expressions; the counterexamples explain why the connector matters.

Independent exercise

A customer can collect an order if identity is verified AND either an order reference OR a collection code is provided. Write the grouped decision with three Boolean constants. Predict the result when identity is false and both references are true.

Correction

Use identityVerified && (hasOrderReference || hasCollectionCode). The predicted result is false: references cannot replace the identity requirement. Parentheses communicate the policy even where precedence might already yield the intended order.

Review

Explain why "false" must not be accepted as the Boolean false. The characters describe text; convert and validate input according to an explicit format instead of relying on truthiness.

Lesson complete?

Your progress is saved on this device.