Logical operators

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — The AND Operator (&&)

The logical AND operator (&&) checks if both conditions are true. If both are true, the result is true. If even one is false, the result is false.

Think of it like this: Your Codémon can enter a tournament only if it has enough level AND enough health points. Both conditions must be met!

let level = 25; let hp = 80; // Both conditions must be true let canEnter = level >= 20 && hp >= 50; console.log("Can enter tournament:", canEnter);

AND Truth Table

true && true → true true && false → false false && true → false false && false → false Both sides must be true to get true!

Exercise 2 — Understanding AND (&&)

What will be the result of the following expression?

let level = 15; let hp = 100; let result = level >= 20 && hp >= 50; console.log(result);
Options
• true
• false
• undefined
• Error

Hint : With the && operator, both conditions must be true for the result to be true. Check each condition: is level >= 20 true? Is hp >= 50 true?

Exercise 3 — The OR Operator (||)

The logical OR operator (||) checks if at least one condition is true. If one or both are true, the result is true. Only if both are false, the result is false.

Think of it like this: Your Codémon can heal at the center if it has low health OR if it's poisoned. Either condition is enough!

let hp = 30; let isPoisoned = false; // At least one condition must be true let needsHealing = hp < 50 || isPoisoned; console.log("Needs healing:", needsHealing);

OR Truth Table

true || true → true true || false → true false || true → true false || false → false At least one side must be true to get true!

Exercise 4 — Understanding OR (||)

What will be the result of the following expression?

let isWaterType = false; let isFireType = true; let result = isWaterType || isFireType; console.log(result);
Options
• true
• false
• undefined
• Error

Hint : With the || operator, at least one condition must be true for the result to be true. Check: is isWaterType true OR is isFireType true?

Exercise 5 — The NOT Operator (!)

The logical NOT operator (!) inverts a boolean value. It turns true into false and false into true. It's like saying "the opposite of".

Think of it like this: If your Codémon is NOT sleeping, it means it's awake. We're checking for the opposite condition!

let isSleeping = false; let isAwake = !isSleeping; console.log("Is sleeping:", isSleeping); console.log("Is awake:", isAwake); // NOT inverts the value let hasBadge = true; console.log("Has badge:", hasBadge); console.log("Needs badge:", !hasBadge);// NOT inverts the value let hasBadge = true; console.log("Has badge:", hasBadge); console.log("Needs badge:", !hasBadge);

Don't confuse ! with !==

! inverts a boolean: !true becomes false !== compares if two values are NOT equal They're different operators with different purposes!

Exercise 6 — Complete the Logical Expression

Complete the code so that canBattle is true only if the Codémon has more than 0 HP AND is not sleeping.

Hint : Both conditions must be true, so use &&. To check if NOT sleeping, use the ! operator before the variable.

Exercise 7 — Healing Center Logic

Complete the code so that needsHealing is true if the Codémon has less than 30 HP OR is poisoned.

Hint : At least one condition needs to be true, so use ||. For "less than", use the < operator.

Exercise 8 — Match Expressions with Results

Match each logical expression with its result.

Items to match
• true && true
• true && false
• false || true
• !true
• false || false
Definitions
• true
• false
• true
• false
• false

Exercise 9 — Combining Logical Operators

What will be the result of this expression?

let level = 25; let hasPotion = true; let isInjured = false; let result = level >= 20 && (hasPotion || !isInjured); console.log(result);
Options
• true
• false
• undefined
• Error

Hint : Work through the expression step by step. First evaluate what's in parentheses: (hasPotion || !isInjured). Then check if the first condition AND the result of the parentheses are both true.

Exercise 10 — Codémon Battle Checker

Create a battle eligibility checker for your Codémon! Your Aquachu wants to battle, but there are several conditions to check: 1. Create a variable canBattle that is true if: - HP is greater than 20 AND - The Codémon is NOT sleeping 2. Create a variable isReady that is true if: - Level is at least 15 OR - Has battle items 3. Log both variables to see the results!

Starter code
let hp = 75; let level = 18; let isSleeping = false; let hasBattleItems = true; // Create your variables here
Back to the course