GameSpark/Tutorials Open editor ↗
JavaScript · Lesson 5

Booleans

A boolean is the simplest type: it is either true or false. Games are full of yes/no facts — is the game over? is the player jumping? did we hit something? — and a boolean holds the answer.

1. true and false

let gameOver = false;
let hasKey = true;

function draw() {
  cls(1);
  print('Game over? ' + gameOver, 8, 8, 7);
  print('Has key? ' + hasKey, 8, 20, 7);
}

2. Comparisons make booleans

Most booleans come from comparing values. Each of these gives back true or false:

let health = 0;

function draw() {
  cls(1);
  print('health > 0 : ' + (health > 0), 8, 8, 8);
  print('health === 0 : ' + (health === 0), 8, 20, 7);
}
💡

Use === to compare (three equals), not =. A single = stores a value; === asks whether two values are equal. Mixing them up is the classic first bug.

3. Combine and flip

Join conditions with && ("and") and || ("or"), and flip one with ! ("not"):

let health = 5;
let hasShield = true;

function draw() {
  cls(1);
  let safe = health > 0 && hasShield;
  print('safe? ' + safe, 8, 8, 11);
  print('in danger? ' + !safe, 8, 20, 8);
}

Booleans are what if statements ask about — you will use them everywhere once you reach If statements next.

What you learned