Variables
Variables are places that store values. There are three kinds of variables in Lua: global variables, local variables, and table fields.
A single name can denote a global variable or a local variable (or a function's formal parameter, which is a particular kind of local variable):
var ::= Name
Name denotes identifiers (see Lexical Conventions).
Any variable name is assumed to be global unless explicitly declared as a local (see Local Declarations). Local variables are lexically scoped: local variables can be freely accessed by functions defined inside their scope (see Visibility Rules).
Before the first assignment to a variable, its value is nil.
Square brackets are used to index a table:
var ::= prefixexp ‘[’ exp ‘]’
The meaning of accesses to table fields can be changed via metatables (see Metatables and Metamethods).
The syntax var.Name
is just syntactic sugar for var["Name"]
:
var ::= prefixexp ‘.’ Name
An access to a global variable x
is equivalent to _ENV.x
. Due to the way that chunks are compiled, the variable _ENV
itself is never global (see Environments and the Global Environment).