pxt-calliope/docs/blocks/variables/var.md
Amerlander 918af4f3ac
Bump V3.0.22 (#110)
* change simulator svg

* change radio image

* Remove google fonts cdn

* change color of 'advanced' button

* font fix

* font fix 2

* display fix

* change fullsceen simulator bg

* Continuous servo

* handle continuous state

* adding shims

* update rendering for continuous servos

* fixing sim

* fix sig

* typo

* fix sim

* bump pxt

* bump pxt

* rerun travis

* Input blocks revision

- add Button and Pin event types
- merge onPinPressed & onPinReleased in new onPinEvent function
- create new onButtonEvent function

* update input blocks in docs and tests

* remove device_pin_release block

* Hide DAL.x behind Enum

* bring back deprecated blocks, but hide them

* shims and locales files

* fix input.input. typing

* remove buildpr

* bump V3

* update simulator aspect ratio

* add Loudness Block

* revoke loudness block

* Adds soundLevel

To be replaced by pxt-common-packages when DAL is updated.

* Remove P0 & P3 from AnalogPin

Co-authored-by: Juri <gitkraken@juriwolf.de>
2020-09-08 02:04:25 -07:00

2.3 KiB

Local Variables

How to define and use local variables.

@parent language

A variable is a place where you can store and retrieve data. Variables have a name, a type, and value:

  • name is how you'll refer to the variable
  • type refers to the kind of data a variable can store
  • value refers to what's stored in the variable

Var statement

Use the Block Editor variable statement to create a variable and the assignment operator to store something in the variable.

For example, this code stores the number 2 in the x variable:

let x = 2;

Here's how to define a variable in the Block Editor:

  1. Click variables.

  2. Change the default variable name if you like.

  3. Drag a block type on the right-side of the assignment operator and click the down arrow to change the variable name.

A variable is created for the number returned by the brightness function.

let b = led.brightness();

Using variables

Once you've defined a variable, just use the variable's name whenever you need what's stored in the variable. For example, the following code shows the value stored in counter on the LED screen:

let counter = 1;
basic.showNumber(counter);

To change the contents of a variable use the assignment operator. The following code sets counter to 1 and then increments counter by 10:

let counter = 1;
counter = counter + 10;
basic.showNumber(counter);

Why use variables?

If you want to remember and modify data, you'll need a variable. A counter is a great example:

let counter = 0;
input.onButtonPressed(Button.A, () => { 
  counter = counter + 1;
  basic.showNumber(counter);
});

Local variables

Local variables exist only within the function or block of code where they're defined. For example:

// x does NOT exist here.
if (led.brightness() > 128) {
  // x exists here
  let x = 0;
}

Notes

  • You can use the default variable names if you'd like, however, it's best to use descriptive variable names. To change a variable name in the editor, select the down arrow next to the variable and then click "new variable".

See also

types, assignment operator