pxt-calliope/olddocs/js/var.md
2016-04-15 14:37:25 -07:00

3.6 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 and let statement

Use the Touch Develop var statement to create a local variable and the assignment operator = to store something in the variable.

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

let num1 = 2

Here's how to define a variable in the Touch Develop editor:

  1. Click var.

  2. Change the default variable name if you like.

  3. Click on the right-side of the assignment operator := and type or click what you want to store in the variable.

The resulting code should look something like this:

// string variable

let name = "Mike"

// number variable

let counter = 1

// boolean variable

let bool = true

// image variable

let img = images.createImage(`
. . # . .
. # # # .
# # # # #
. # # # .
. . # . .
`)

See Image for info on creating and using image variables.

Store in var button

Another way to define a variable is to use the store in var button. Here's how:

  • in the Touch Develop editor, click a function button that returns a value (i.e. led -> brightness)
  • click store in var
  • click rename to change the default variable name

The resulting code should look something like this:

let brightness = led.brightness()

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

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:

basic.showNumber(counter, 100)

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

counter = 1
counter = counter + 10

Why use variables?

Variables help simplify your code. For example, instead of turning on LEDs one by one like this:

led.plot(0, 0)
led.plot(1, 1)
led.plot(2, 2)
led.plot(3, 3)
led.plot(4, 4)

You can use a variable (i) and a for loop to plot the same series of points (i is incremented by 1, each time the loop repeats):

for (let i = 0; i < 5; i++) {
    led.plot(i, i)
}

Local vs global variables

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

if (led.brightness() > 127) {
    let y = 1
    // `y` variable exists here
} else {
    // `y` variable does not exist here
}

Use global variables when you need to access a variable in nested code blocks or across multiple functions.

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 variable and then click rename.
  • Be careful not to confuse the assignment := operator with the equals = operator.

Lessons

guess the number, digi yoyo, rock paper scissors, love meter

See also

global variables, types, assignment operator