pxt-calliope/docs/reference/js/var.md

145 lines
3.6 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Local Variables
2016-04-02 01:22:47 +02:00
How to define and use local variables.
2016-03-26 00:47:20 +01:00
### @parent language
2016-04-13 17:27:45 +02:00
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/js/types), and value:
2016-03-26 00:47:20 +01:00
* *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
2016-04-13 17:27:45 +02:00
Use the Touch Develop *var* statement to create a local variable and the [assignment operator](/reference/variables/assign) `=` to store something in the variable.
2016-03-26 00:47:20 +01:00
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.
2016-04-13 17:27:45 +02:00
3. Click on the right-side of the [assignment operator](/reference/variables/assign) `:=` and type or click what you want to store in the variable.
2016-03-26 00:47:20 +01:00
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(`
. . # . .
. # # # .
# # # # #
. # # # .
. . # . .
`)
```
2016-04-13 17:27:45 +02:00
See [Image](/reference/image/image) for info on creating and using image variables.
2016-03-26 00:47:20 +01:00
### Store in var button
Another way to define a variable is to use the `store in var` button. Here's how:
2016-04-13 17:27:45 +02:00
* in the [Touch Develop editor](/js/editor), click a function button that returns a value (i.e. `led` `->` `brightness`)
2016-03-26 00:47:20 +01:00
* click `store in var`
* click `rename` to change the default variable name
The resulting code should look something like this:
```
let brightness = led.brightness()
```
2016-04-13 17:27:45 +02:00
A variable is created for the number returned by the [brightness](/reference/led/brightness) function.
2016-03-26 00:47:20 +01:00
### 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)
```
2016-04-13 17:27:45 +02:00
You can use a variable (`i`) and a [for loop](/reference/loops/for) to plot the same series of points (`i` is incremented by 1, each time the loop repeats):
2016-03-26 00:47:20 +01:00
```
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
}
```
2016-04-13 17:27:45 +02:00
Use [global variables](/js/data) when you need to access a variable in nested code blocks or across multiple functions.
2016-03-26 00:47:20 +01:00
#### 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
2016-04-13 17:27:45 +02:00
[guess the number](/lessons/guess-the-number), [digi yoyo](/lessons/digi-yoyo), [rock paper scissors](/lessons/rock-paper-scissors), [love meter](/lessons/love-meter)
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[global variables](/js/data), [types](/js/types), [assignment operator](/reference/variables/assign)
2016-03-26 00:47:20 +01:00