Variables


A variable is a named container than stores a value. Defining a variable follows the pattern:

[name] = [value]       -- globally scoped

local [name] = [value] -- locally-scoped variable

where name is the name of the variable, and value is the value assigned to that variable.

Note

It can sometimes be helpful to additionally think of this a bit more technically: Each value is stored somewhere in a small piece of computer memory, and the name is used to reference the value that is stored there.

The second example above begins with the local keyword, which declares that this variable is defined in the "local" scope. We will learn more about scope in the next section, but for now just know that it is good practice to precede variable declarations with the local keyword, so that is what we will do in our examples.

Some examples of variable definitions are:

local name = "John"

local age = 23

local height = 182.3

local x  -- nil

These examples show how to define variables that contains strings, integers, and floats, respectively. Note in each case that the name of the variable is on the left, while the value assigned to it is on the right. In the last example the variable x was defined, but no value was assigned to it. In this case x is assigned the default value nil, which we will learn about shortly.

Lua does not require that the variable type be declared, the type is inferred from the value assigned to the variable.

After a variable has been created, the name of the variable represents the value assigned to it in subsequent operations. For example, one can perform arithmetic on variables:

print(age + 3) -- 26

Reassignment

After a variable has been created, new values can be assigned to that variable:

local x = 123

print(x) -- 123

x = 456

print(x) -- 456

Note that because we had already declared the x variable in the local scope, although the local keyword in not used when we assign 456 to that variable it remains in the local scope.

Multiple-Assignment

Lua also supports "multiple" assignment, meaning that multiple values can be assigned on the same line. The example above could be re-written:

local name, age, height = "John", 23, 182.3

When multiple-assignment is used, each of the values on the right side of the = are evaluated, then they are assigned to the variables on the left side in-order. There are a few cases to be aware of:

More Variables Than Values

When there are more variables than there are values, missing values are treated as nil.

local name, age, height = "John", 23

print(name) -- John
print(age) -- 23
print(height) -- nil

Fewer Variables Than Values

When there are fewer variables than there are values, only the left-most values are assigned:

local name, age, height = "John", 23, 182.3, "blue", "brown"

print(name) -- John
print(age) -- 23
print(height) -- 182.3
-- "blue" and "brown" were ignored

When to use Multiple-Assignment

In most cases, multiple assignment makes code more difficult to read, and more difficult to comment, so it should generally be avoided. However, there is one case where it can be helpful: Suppose we want to swap values assigned to two variables:

local x0 = 3
local x1 = 2

if x0 > x1 then
    x0, x1 = x1, x0 -- swap values assigned to variables
end

print(x0) -- 2
print(x1) -- 3

This is one of the few times where it makes sense to use multiple-assignment directly.

However, as we will see in coming chapters, multiple assignment is very common when returning values from functions, so it is important to understand the basic mechanics of how it works.