Skip to content

Variables

Use the Variable blocks to store a value that the code can refer to later.

Example 1

In this example, we create the variable diceValue and set it to a random number between 1 and 6. The value of diceValue is then displayed in a text output called diceDisplay. A Digits display shows the text output.

Example 2

This example contains two variables. The first variable notesList stores a list of numbers. The second variable j is defined as the item in each list. The first time the loop circles through, j is 261; the second time, it's 327; and the third, it's 392.

Example 3

If a variable is a number, you can use the change by block to increase or decrease the value of the variable. In this example, the variable button1Count increases by 1 each time you press Button 1 on the //code.Node.

Note

Set the value of button1Count to 0 at the beginning of the code, since Blockly doesn't assume that a variable starts at 0.