← Level 2 – Wizard

Lesson 3 of 8 ⏱ 30 minutes

Variables and conditions

Teach your page to remember things and make decisions. This is the heart of programming.

Show me: variables – the program’s memory

A variable is a labeled box where a program stores a value:

let clickCount = 0;        // number
let name = "Jake";         // string (text)
let isWeekend = true;      // true/false

You can do math with variables and change them:

clickCount = clickCount + 1;   // increase by 1
clickCount++;                  // same thing, shorter

Show me: a click counter

Let’s combine a variable with the button from the last lesson:

<button id="counter">Clicked: 0×</button>
const counter = document.getElementById("counter");
let count = 0;

counter.addEventListener("click", () => {
  count++;
  counter.textContent = "Clicked: " + count + "×";
});

For the first time, the page remembers something. Every click increases the number in the box and displays it.

Show me: conditions – making decisions

A condition says: “If this is true, do A. Otherwise, do B.”

if (count >= 10) {
  counter.textContent = "Ten clicks! You've got some serious stamina 🏆";
} else {
  counter.textContent = "Clicked: " + count + "×";
}

Your turn 💪

Build a counter with a surprise: after a certain number of clicks, something extra happens.

🏫 School website – vote for the best cafeteria food
<h2>Vote for pizza day!</h2>
<button id="vote">Vote 🍕 (0 votes)</button>
<p id="result"></p>
const vote = document.getElementById("vote");
const result = document.getElementById("result");
let votes = 0;

vote.addEventListener("click", () => {
  votes++;
  vote.textContent = "Vote 🍕 (" + votes + " votes)";

  if (votes >= 5) {
    result.textContent = "Decided! Pizza day wins the election. 🏆";
  }
});
🙋 Personal website – juggling counter
<h2>How many times can I juggle the ball?</h2>
<button id="kick">Kick! (0)</button>
<p id="record"></p>
const kick = document.getElementById("kick");
const record = document.getElementById("record");
let kicks = 0;

kick.addEventListener("click", () => {
  kicks++;
  kick.textContent = "Kick! (" + kicks + ")";

  if (kicks === 20) {
    record.textContent = "20 kicks – new personal record! 🎉";
  }
});
🌍 Travel diary – visited countries counter
<h2>Country counter</h2>
<button id="country">Add a country (0)</button>
<p id="rating"></p>
const country = document.getElementById("country");
const rating = document.getElementById("rating");
let countryCount = 0;

country.addEventListener("click", () => {
  countryCount++;
  country.textContent = "Add a country (" + countryCount + ")";

  if (countryCount >= 10) {
    rating.textContent = "Ten countries – you're a globetrotter! 🌍";
  } else if (countryCount >= 5) {
    rating.textContent = "Five countries – beginner traveler. 🎒";
  }
});

Notice the else if – this is how you chain multiple conditions together.

⚽ Club website – score tracker
<h2>Shooting practice</h2>
<button id="goal">GOAL! ⚽</button>
<p id="score">Score: 0</p>
const goal = document.getElementById("goal");
const score = document.getElementById("score");
let goals = 0;

goal.addEventListener("click", () => {
  goals++;

  if (goals >= 10) {
    score.textContent = "Score: " + goals + " – hat trick and more! Player of the match 🏆";
  } else {
    score.textContent = "Score: " + goals;
  }
});
💡

You just learned real programming. Variables and conditions aren’t just “web stuff” – they work exactly the same way in Python, in games, and in apps. Anyone who knows variables and conditions can talk to any programming language in the world.

Check yourself: Your counter counts, and once it hits the limit, something extra happens. Try explaining out loud what the code does, line by line – if you can do that, you’ve got the basics down.

What to take away from this lesson