Booleans Lesson

A boolean is a value that can only be true or false. That sounds small, but a boolean can control whether a door opens, whether a menu is visible, whether a character is alive, or whether a button can be clicked. Boolean variables are easiest to read when their names sound like yes-or-no statements. Names like hasKey, gameOver, menuOpen, and isJumping make the code understandable before anyone even reads the condition.

Working Sample

Code Runner Challenge

Booleans Lesson

View IPYNB Source
%%js

// CODE_RUNNER: Booleans Lesson

let hasKey = true;
let doorLocked = true;

if (hasKey && doorLocked) {
  doorLocked = false;
  console.log("The door opens.");
} else {
  console.log("The door stays closed.");
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

The following code uses two booleans to represent a simple lock situation. The program only changes the door state when both conditions match the rule.