COMPUTER PROGRAMMING 1 (JAVASCRIPT) • CONDITIONALS, LOOPS & COMMON PATTERNS

Compound Conditions — I can evaluate compound conditions using &&, ||, and ! and predict outcomes at my level.

Combine simple true/false checks into powerful decision-making logic using AND, OR, and NOT operators.

Historical Context & Motivation

Every time you use a search engine, unlock your phone, or play a video game, the software behind the scenes is making hundreds of decisions per second. Many of those decisions are not simple yes-or-no checks — they combine multiple conditions at once. For example, a login system might need to verify that a username is correct and that a password matches and that the account is not locked. This kind of multi-part logic is called a compound condition, and it sits at the heart of how programs make complex decisions.

The logical operators that power compound conditions — AND, OR, and NOT — were not invented by programmers. They trace back to formal logic and mathematics, long before the first computer was ever built. Understanding their origins helps you see that these operators follow precise, predictable rules rather than arbitrary syntax.

1847
George Boole Publishes Boolean Algebra
English mathematician George Boole introduced a system of logic using only two values — true and false — and the operations AND, OR, and NOT. This became the mathematical foundation for every digital circuit and programming language.
1938
Claude Shannon Links Logic to Circuits
Shannon's MIT thesis proved that Boolean algebra could be implemented with electrical switches, connecting abstract logic to real hardware. Every computer chip you use today relies on this insight.
1972
C Language Introduces && and ||
The C programming language formalized the double-ampersand (&&) for AND and double-pipe (||) for OR. These symbols carried forward into Java, JavaScript, C++, and many other languages.
1995
JavaScript Adopts the Same Operators
When Brendan Eich created JavaScript, he adopted the &&, ||, and ! syntax directly from the C family. Today, these operators power billions of web pages and apps around the world.

So the core question this lesson addresses is straightforward: when your program needs to check more than one thing before making a decision, how do you combine those checks correctly, and how do you predict what the combined result will be? Mastering &&, ||, and ! gives you that power.

Core Principles & Definitions

Before diving into code, you need to understand three foundational ideas. Each of the logical operators takes Boolean values (true or false) as input and produces a single Boolean value as output. Think of them as decision-making tools that combine smaller yes/no questions into a bigger yes/no answer.

1

AND Operator (&&)

Returns true only when both conditions on either side are true. If even one side is false, the whole expression is false. Written as && in JavaScript.
2

OR Operator (||)

Returns true when at least one condition is true. It is only false when both sides are false. Written as || in JavaScript.
3

NOT Operator (!)

Flips a single Boolean value to its opposite. !true becomes false, and !false becomes true. It is a unary operator — it only needs one operand.
4

Operator Precedence

When multiple operators appear in one expression, JavaScript evaluates ! first, then &&, then ||. Parentheses can override this order, just like in math.
5

Short-Circuit Evaluation

JavaScript is efficient: with &&, if the left side is false, the right side is never checked. With ||, if the left side is true, the right side is skipped. This is called short-circuit evaluation.
✦ KEY TAKEAWAY
Think of && like a strict parent: both conditions must pass to get a "yes." Think of || like a lenient parent: if either condition passes, you get a "yes." And ! is like flipping your answer upside-down — whatever it was, it becomes the opposite.

Visual Explanation — Truth Tables as Logic Gates

The best way to see how compound conditions work is through a truth table — a chart that lists every possible combination of true and false inputs and shows the resulting output. The diagram below displays truth tables for all three operators side by side, so you can compare their behavior at a glance.

The truth tables above show every possible input combination for each operator. Notice that && produces true in only one row, while || produces true in three out of four rows. The precedence bar at the bottom reminds you of the evaluation order.

Study the color coding carefully. Green results represent true outputs and red results represent false outputs. The dramatic difference between the AND and OR tables is the most important takeaway: AND is strict (mostly false), while OR is lenient (mostly true). The NOT table is the simplest — it always produces the opposite of whatever it receives.

How Compound Conditions Work in Code

Now let's translate the abstract logic into real JavaScript. A compound condition is simply two or more comparison expressions joined by && or ||, optionally with ! flipping individual parts. Each comparison expression (like age >= 16) evaluates to a Boolean, and then the logical operator combines those Booleans.

AND PATTERN
condition1 && condition2
Both condition1 and condition2 must be true for the entire expression to be true. Example: age >= 16 && hasPermit === true
OR PATTERN
condition1 || condition2
At least one of condition1 or condition2 must be true. Example: day === "Saturday" || day === "Sunday"
NOT PATTERN
!condition
Flips the Boolean value of condition. Example: !isLoggedIn is true when the user is NOT logged in.
COMBINED PATTERN
!(cond1) && (cond2 || cond3)
Parentheses group the OR check first. The NOT flips cond1. Then AND requires both the flipped result and the OR group to be true. Always use parentheses when mixing operators to make your intent clear.
⚡ Short-Circuit Tip
With &&, if the left side evaluates to false, JavaScript immediately returns false without checking the right side — because nothing can save an AND once one part fails. Similarly, with ||, if the left side is true, the right side is skipped — because the OR is already satisfied. This is called short-circuit evaluation and it can improve performance and prevent errors.

Detailed Breakdown — Evaluating Complex Expressions

Real programs rarely use just one operator at a time. You will often see expressions that combine two or even three logical operators. The key to evaluating these correctly is to follow the order of operations: NOT first, then AND, then OR — unless parentheses override the order. The following diagram walks through a complex expression step by step.

This flowchart traces the expression x > 5 && !z || y <= 10 with x = 10, y = 5, z = true. Each step applies one operator according to precedence: NOT first, then AND, then OR. The final result is true.
Evaluating the same expressions with different variable values
Expressionx = 10, y = 5, z = truex = 3, y = 5, z = true
x > 5 && y < 10true && true → truefalse && true → false
x > 5 || y < 10true || true → truefalse || true → true
!z && x > 5false && true → falsefalse && false → false
!(x > 5 && y < 10)!(true && true) → !(true) → false!(false && true) → !(false) → true

The table above reinforces a critical skill: substitute the actual values, evaluate each comparison to true or false, then apply the logical operators in precedence order. Practice this mechanical process and you will be able to predict the outcome of any compound condition.

Worked Example — Ticket Pricing System

Imagine you are building a movie ticket pricing system. A customer gets a discount if they are a student AND the showing is before 5 PM, OR if they are a senior citizen. Let's write and evaluate the compound condition step by step.

Movie Ticket Discount Checker
1
Step 1 — Identify the VariablesWe have three variables: let isStudent = true; — the customer is a student. let showTime = 14; — the movie starts at 2 PM (24-hour format). let isSenior = false; — the customer is not a senior citizen.
2
Step 2 — Write the Compound ConditionThe discount rule translates to: (isStudent && showTime < 17) || isSenior. We use parentheses around the AND portion to make it clear that the student-and-time check is grouped together, and the OR with senior is separate.
3
Step 3 — Substitute ValuesReplacing each variable with its value: (true && 14 < 17) || false. Next, evaluate the comparison: 14 < 17 is true. So we have: (true && true) || false.
(true && true) || false
4
Step 4 — Evaluate the AND (inside parentheses first)true && true evaluates to true because both sides are true. Now we have: true || false.
true || false
5
Step 5 — Evaluate the ORtrue || false evaluates to true because at least one side is true. The customer gets the discount!
Final result: true — discount applies ✓
🔄 What If the Values Change?
Try substituting isStudent = false and isSenior = false. You'd get (false && true) || false, which simplifies to false || false, which is false. No discount! This shows how a single variable change can flip the entire result.

Comparing the Three Operators

Now that you have seen all three operators in action, it helps to compare them side by side. The table below highlights when to use each operator, common mistakes, and tips for keeping your code readable.

Side-by-side comparison of JavaScript logical operators
Feature&& (AND)|| (OR)! (NOT)
Number of operandsTwo (binary)Two (binary)One (unary)
Returns true whenBoth are trueAt least one is trueThe operand is false
Returns false whenAt least one is falseBoth are falseThe operand is true
Short-circuits whenLeft side is falseLeft side is trueN/A (single operand)
Common use caseChecking multiple requirementsChecking alternative optionsInverting a Boolean flag
Common mistakeUsing & instead of &&Using | instead of ||Forgetting parentheses with complex expressions
✦ KEY TAKEAWAY
Think of && like an airport security checkpoint: you need a valid ID and a boarding pass to get through — missing either one stops you. Think of || like paying for lunch: you can use cash or a card — either one works. And ! is like an on/off switch: whatever state it is in, the NOT operator flips it to the other state.

Connection to Advanced Logic & Patterns

Compound conditions are your entry point into a broader world of logical reasoning in programming. As you progress, you will encounter these operators in increasingly sophisticated contexts, from loop conditions to data validation to software architecture patterns. The table below previews how what you learn now connects to more advanced topics.

How compound conditions connect to advanced programming concepts
What You Learn NowWhere It Leads
Combining two conditions with && and ||Complex conditionals in loops: while (hasLives && !gameOver)
Using ! to negate conditionsDe Morgan's Laws: !(A && B) === !A || !B — simplifying complex logic
Operator precedence (!, &&, ||)Understanding full operator precedence tables with arithmetic, comparison, and logical operators combined
Short-circuit evaluationGuard clauses and null checks: user && user.name prevents errors by checking if user exists before accessing .name
Predicting true/false outcomesWriting unit tests with assertions that verify your conditions work correctly for every input combination

One particularly useful connection is De Morgan's Laws, named after the 19th-century mathematician Augustus De Morgan. These laws state that !(A && B) is always equivalent to !A || !B, and !(A || B) is always equivalent to !A && !B. You don't need to memorize these now, but they are a natural extension of the skills you are building in this lesson.

Practice Problems

PROBLEM 1 — CONCEPTUAL
Explain in your own words why the expression true && false evaluates to false, while true || false evaluates to true. What is the fundamental difference between how && and || make their decision?
PROBLEM 2 — BASIC CALCULATION
Given let a = 8; let b = 3; let c = true;, evaluate the following expression step by step: a > 5 && b < 10 && !c. What is the final result?
PROBLEM 3 — INTERMEDIATE
Given let score = 72; let hasExtraCredit = true; let isAbsent = false;, evaluate: (score >= 70 && !isAbsent) || (hasExtraCredit && score >= 60). Show your work for each step and state the final result.
PROBLEM 4 — APPLIED
You are writing a function for a ride-sharing app. A driver can accept a ride request if they are currently online AND have at least 4.5 stars AND (the ride distance is under 15 miles OR they have opted into long rides). Write the compound condition as a JavaScript expression using variables isOnline, rating, distance, and longRideOptIn. Then evaluate it when isOnline = true, rating = 4.8, distance = 22, longRideOptIn = false.
PROBLEM 5 — CRITICAL THINKING
Consider the expression !(x > 10 && y < 5). Using De Morgan's Law (mentioned in Section 8), rewrite this expression as an equivalent expression that does NOT use the outer NOT operator. Then test both versions with x = 12, y = 3 and with x = 7, y = 8 to confirm they produce the same results.

Lesson Summary

Compound conditions let you combine multiple Boolean checks into a single decision. The && (AND) operator returns true only when both sides are true — think of it as strict gatekeeper logic. The || (OR) operator returns true when at least one side is true — it's the lenient option. The ! (NOT) operator flips any Boolean to its opposite. When these operators appear together, remember the precedence order: NOT first, then AND, then OR, and use parentheses to make your intent crystal clear.

To predict the outcome of any compound condition, follow this process: substitute variable values, evaluate each comparison to true or false, apply NOT operators, then evaluate AND operators, and finally evaluate OR operators. Understanding short-circuit evaluation adds another layer of insight: JavaScript skips unnecessary checks to save time. These skills form the foundation for writing loops, handling user input, validating data, and eventually mastering patterns like De Morgan's Laws that simplify complex logic.

Varsity Tutors • Computer Programming 1 (JavaScript) • Compound Conditions — I can evaluate compound conditions using &&, ||, and ! and predict outcomes at my level.