Local Declarations

Local variables can be declared anywhere inside a block to limit the variable's scope. In addition to limiting the scope, accessing a local variable is faster than accessing a global variable. For both of these reasons, local variables should be used whenever possible. The declaration can include an initial assignment:

      statement ::= local namelist [`=´ explist]
local x
local y = 12345

If present, an initial assignment has the same semantics of a multiple assignment (see Assignment). Otherwise, all variables are initialized with null.

local a,b = 'abc', 'xyz'

A chunk is also a block (see Chunks), and so local variables can be declared in a chunk outside any explicit block. The scope of such local variables extends until the end of the chunk.

The visibility rules for local variables are explained in Visibility Rules.