➕ Operators

Boolean Expressions

Project Evidence Required
Compound conditions in game logic.
Assessment Method
Code review: &&, ||, !

What it is

Boolean operators combine truthy/falsy values: && (AND, true only if both sides are true), || (OR, true if either side is true), ! (NOT, flips the value). They short-circuit, meaning JavaScript stops evaluating as soon as the result is determined.

Why it matters

Game logic rarely depends on a single condition. ‘Player is alive AND not paused AND in range’ is a compound boolean. Short-circuiting also lets you write defensive checks like obj && obj.method && obj.method() to avoid null-reference errors.

How GateGame implements it

Damage gating combines multiple conditions: if (collision && !this.isVulnerable && enemy.isAlive). Input checks use OR: if (key === 'ArrowUp' || key === 'w'). Defensive method calls use short-circuit AND: obj.data.interact && obj.data.interact() ensures we don’t crash on entities without an interact method.

Code Example

// Damage gate: ALL three conditions must hold
if (collision && !this.isVulnerable && enemy.isAlive) {
  this.takeDamage();
}

// Multiple keys map to same action
if (key === 'ArrowUp' || key === 'w' || key === ' ') {
  player.jump();
}

// Short-circuit guard against missing method
obj.data.interact && obj.data.interact();

Key Takeaway

Compound booleans express game rules clearly. Short-circuiting (a && b.method()) is a defensive coding superpower.