Variables are like containers/placeholders for values. Variables are a foundational programming concept that enables the creation of advanced scripts. Scratch is great for learning to create and manipulate variables.
You’ve likely encountered variables before. Here are some examples:
x = 5
name = "Sarah"
gameLoading = true
Code language: JavaScript (javascript)
Variables are found in the Variables category in Scratch. Clicking on Variables brings up the variables blocks:
Next, click on the Make a Variable button to create your variable.
After clicking that button, you’ll need to name your variable. For now, leave the For all sprites option clicked. This creates a global variable that you can use anywhere in your project. The alternative is a local variable. The difference between these options is outside of the scope of this course.
Your variable is now available for use! This image shows a variable named “health” that can now be used in other scripts. This name matches what you provide in the previous step.
Here’s an example of using variables in a script: