Variables

Make a Variable

Variables are made (declared) in the “Global Variables” TAB. If you don’t have that TAB then use the “+” to add it to your project. You don’t have to declare these Global Variables in your script (js file) like normal JavaScript, however, you may declare variables in your script file as per normal JavaScript if you wish. Do not declare the same variable twice because it won’t run.

Variable Types:

  • String: “word” or “text” eg ‘clicked’
  • Number: mathematical number eg 24
  • Boolean: value can be either “true” or “false” but this is NOT a “string” variable.
  • Array: same as Scratch3 “List”

Set Variable

my_variable = 0

Change Variable

my_variable = my_variable + 1

Display Variables

Variables are displayed or hidden on the Scratch Stage by selecting/deselecting the “Display” box on the right hand side of that variable in the “Global Variables” TAB.


Example

The variable “score” is declared as a “number” and checked to display on the stage.

// Example: variables
score = 0
example_Sprite_sprite.show()
    example_Sprite_sprite.say('click me to add 1 to displayed score variable')
    example_Sprite_sprite.whenClicked(function (){
        score = score + 1
    })