Variables

A free sample lesson from Scratch Coding

The idea

Think of a variable like a labeled jar that remembers how many marbles are inside it - 'set to 0' empties the jar and starts over, 'change by 1' drops one more marble in without touching the ones already there.

What it actually is

A Variable is a named box that remembers a value - like a score - across the whole time a project runs. 'Make a Variable' creates one, 'set [variable] to (0)' gives it a starting value, and 'change [variable] by (1)' adds to whatever it currently holds.

How you write it

set [score] to (0)
change [score] by (1)

A worked example

when 🏳️ clicked
set [score] to 0

when this sprite clicked
change [score] by 1
say (join "Score: " (score)) for 1 seconds

The green-flag script resets score to 0 at the very start. Every time the sprite is clicked, its own script adds 1 to score and shows the new total - the variable remembers its value across every single click, not just the current one.

Mistakes children actually make

Mistake 1: forgetting to 'set' a variable to a starting value when the project begins - without a reset, a score can start at whatever it was left at from the LAST time the project ran. Mistake 2: mixing up 'set' (replaces the value completely) and 'change by' (adds to whatever's already there) - using 'set 1' repeatedly never actually adds up.

Then they try it

In the app this lesson continues with the animated explanation, spoken aloud, and then the practice: Make a variable, set it to a starting value, and change it by some amount. The editor runs your child’s real code and checks the result, with their coding buddy reacting to what they wrote.

Try it free