pxt-calliope/olddocs/js/assign.md

1.4 KiB

Assignment Operator

Set the value for local and global variables.

@parent js/operators

Set or change the value of a variable

Block Editor

Touch Develop

Use the assignment operator (:=) to set or change the value of a local variable or a global variable.

Declare a variable

Declare a new local variable using the var statement and the assignment operator (:=). Like this:

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

See global variable for info on declaring a global variable.

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.
  • You can use the assignment operator := with variables of each of the supported types.

Lessons

counter, rotation animation, digital pet, offset image

See also

local variables, global variables, types