2016-03-26 00:47:20 +01:00
|
|
|
# Assignment Operator
|
|
|
|
|
2016-04-02 01:22:47 +02:00
|
|
|
Set the value for local and global variables.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
### @parent js/operators
|
|
|
|
|
|
|
|
|
|
|
|
Set or change the value of a variable
|
|
|
|
|
|
|
|
### Block Editor
|
|
|
|
|
|
|
|
![](/static/mb/antenna-0.png)
|
|
|
|
|
|
|
|
### Touch Develop
|
|
|
|
|
2016-04-13 17:27:45 +02:00
|
|
|
Use the assignment operator (:=) to set or change the value of a [local variable](/reference/variables/var) or a [global variable](/js/data).
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
### Declare a variable
|
|
|
|
|
2016-04-13 17:27:45 +02:00
|
|
|
Declare a new *local* variable using the [var](/reference/variables/var) statement and the assignment operator (`:=`). Like this:
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
let num1 = 7
|
|
|
|
let name = "Joe"
|
|
|
|
```
|
|
|
|
|
|
|
|
The variable's name is on the left of the assignment operator (`:=`) and the variable's value is on the right:
|
|
|
|
|
|
|
|
*variable name* `:=` *value*
|
|
|
|
|
2016-04-13 17:27:45 +02:00
|
|
|
See [global variable](/js/data) for info on declaring a global variable.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
### Change a variable
|
|
|
|
|
|
|
|
After a global or local variable is defined, use the assignment operator (`:=`) to change the variable's value.
|
|
|
|
|
|
|
|
```
|
|
|
|
g = 42
|
|
|
|
num1 = 42
|
|
|
|
```
|
|
|
|
|
|
|
|
### Notes
|
|
|
|
|
|
|
|
* Don't confuse the assignment operator `:=` with the equality operator `=`, which is used to compare values.
|
2016-04-13 17:27:45 +02:00
|
|
|
* You can use the assignment operator `:=` with variables of each of the supported [types](/js/types).
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
### Lessons
|
|
|
|
|
2016-04-13 17:27:45 +02:00
|
|
|
[counter](/lessons/counter), [rotation animation](/lessons/rotation-animation), [digital pet](/lessons/digital-pet), [offset image](/lessons/offset-image)
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
### See also
|
|
|
|
|
2016-04-13 17:27:45 +02:00
|
|
|
[local variables](/reference/variables/var), [global variables](/js/data), [types](/js/types)
|
2016-03-26 00:47:20 +01:00
|
|
|
|