The assignment statement evaluates an expression and assigns the resulting value to a variable.

variable = expression

As noted earlier, values of any type can be assigned to any variable, so the following is allowed:

local v1 = "a string literal" v1 = 10

An assignment statement can actually set multiple variables, using the form

variable1, variable2, … = expression1, expression2, …

If there are more variables than expressions, the extra variables are assigned nil. If there are more expressions than variables, the extra expression values are discarded. The expressions are all evaluated before the assignments, so this can be used to succinctly exchange the values of two variables:

v1, v2 = v2, v1

is equivalent to

tmp = v1 v2 = v1 v1 = tmp


In this article