pxt-calliope/olddocs/js/var.md

109 lines
2.6 KiB
Markdown
Raw Permalink 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-16 00:02:26 +02:00
The ``var`` keyword declares a global variables that is defined within the entire scope of the function.
The ``let`` keyword defined a block-scoped variable, similarly to other languages like Java, C# or C.
2016-03-26 00:47:20 +01:00
For example, this code stores the number `2` in the `num1` variable:
2016-04-16 00:02:26 +02:00
* number variable
```blocks
2016-03-26 00:47:20 +01:00
let num1 = 2
```
2016-04-16 00:02:26 +02:00
* string variable
```blocks
2016-03-26 00:47:20 +01:00
let name = "Mike"
```
2016-04-16 00:02:26 +02:00
* boolean variable
2016-03-26 00:47:20 +01:00
2016-04-16 00:02:26 +02:00
```blocks
2016-03-26 00:47:20 +01:00
let bool = true
```
2016-04-16 00:02:26 +02:00
* image variable
```blocks
2016-03-26 00:47:20 +01:00
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
### 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:
```
2016-04-16 00:02:26 +02:00
let counter = 5;
2016-03-26 00:47:20 +01:00
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:
```
2016-04-16 00:02:26 +02:00
let counter = 0;
2016-03-26 00:47:20 +01:00
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-16 00:02:26 +02:00
Use **global variables** when you need to access a variable in nested code blocks or across multiple functions.
2016-03-26 00:47:20 +01:00
### 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-16 00:02:26 +02:00
[types](/reference/types), [assignment operator](/reference/variables/assign)
2016-03-26 00:47:20 +01:00