< Comments | UserGuide | Tables >

Variables and values

A variable is name with which a value is associated. For example, the variable PI stands for a number that is used for calculations when working with circles. PI is the variable, 3.14(...) is the value. Variables store information that we need to work with. For example, I could use a variable to store a name:

name = "jack"

The variable name holds now the name jack.

However, any variable is also a value:

jack = "name"

In Lua, any value can be associated withanother value, turning the value it is associated with into a variable.

A value does not know about its variables it is associated to! Only the variables know their value.

Variables in Lua do not have to be declared, however we can decide wether a variable should be global or local. Local means, that the variable is only visible within a certain range of the code, while global variables can be accessed everywhere (as long as the programmer allows this).

In Lua, most things we use are just values. The string we use as variable to describe a value is a value itself. Numbers, functions, tables - all these types are values that can be also used as variables, thus can be associated with a value.