This tutorial explains what variables are, how to make them, what one can do with them, and how to use them in one's projects. Variables in Scratch consist of a series of symbols to represent a value; mathematically they are symbols that represent numbers.

## What are Variables?

Main article: Variable

A simple way to think about variables is a box with numbers in it. These numbers can be increased and decreased and made to control various parts of a project. Contrary to algebraic variables (which are usually unknown), the variables in Scratch and other programming languages are simply known values. In fact, in Scratch 1.3 and above, variables can contain text (strings), numbers, or booleans (true/false values). Some examples are below:

• Hello, world
• 123
• 3.14
• 0
• -321
• true/false
• {nothing — empty string}

## How are Variables made?

In the variables palette, click . A box will appear. Type the name of the wanted variable, and select whether it should be "for all sprites" (global) or "for this sprite only" (private). Press OK.

## What can be done with Variables?

Variables have theoretically infinite uses. One of the most common is to simply store values — this only requires the Set () to () block and is not hard at all.

Another common use is to create efficient scripts. As a variable's value can change, variables are often used in blocks that contain number or text input.

A common example is velocity: Here is a simple script for a falling sprite in an animation project. It works, but the script is inefficient:

when I receive [Fall v]
change y by (-1)
wait (0.1) secs
change y by (-2)
wait (0.1) secs
change y by (-3)
wait (0.1) secs
change y by (-4)
wait (0.1) secs
change y by (-5)
wait (0.1) secs
change y by (-6)
wait (0.1) secs
change y by (-7)
wait (0.1) secs
change y by (-8)
wait (0.1) secs
change y by (-9)
wait (0.1) secs
change y by (-10)

The script performs the same action ten times, except that the value changes at a constant speed. A variable could be used there:

when I receive [Fall v]
set [Speed v] to [0]
repeat (10)
change [Speed v] by (-1)
change y by (Speed)
wait (0.1) secs

Both scripts work the same way, but the latter is more efficient, because it uses variables and it shortens the length of the script.

Variables can also be used to store Cloud Data. This can be useful in multiplayer games, as it allows Scratchers to interact with each other.

## How can Variables be used with their Stage Monitors?

Another common use for variables is for displays - as each variable gets its own Stage monitor, it makes variable displays very easy. Stage monitors can serve a variety of purposes:

• Displaying health
• Displaying score
• Showing completion of levels (Example: 1/5 Missions completed)
• Showing time
• Displaying speed of movement
• Adjustable Variables for interactive games

Health displays are very simple. They only take a few steps:

• When the project is started (i.e. the Green Flag is clicked), the variable must be set to the maximum amount of health
• When the player gets hurt, the variable must decrease
• When the player gets healed, the variable must increase

Here is a simple script to perform these actions — there are many different types of games with health, so it should be changed for one's own uses:

when gf clicked
set [Health v] to (10)
forever
if <touching [Lava v]?> then
change [Health v] by (-1)
end
if <touching [Healer v]?> then
change [Health v] by (1)
end

In order for the variable to be displayed on the stage, the box next to the variable being displayed in the variable block palette must must be checked:

When a variable is created, it will show on the stage by default.

The Show Variable () and Hide Variable () blocks can also be used to control this within a script.

## How can Variables be Deleted?

To delete a variable, right click on the variable and hit delete.
Deleting a variable.
 Note: Variable usage is automatically deleted as of Scratch 3.0, but remember to brush up the blocks around it.

## How can Variables be Renamed?

To rename a variable, right click on the variable and hit rename.
Renaming a variable.