Variable Scope

Local

Variables have different scopes. Once the end of the scope is reached the values in that scope are no longer accessible

function foo()
  local a = 10
end

print(a)
―――――
nil

Global

Global variables do not need declarations. You simply assign a value to a global variable to create it.

b = 10
print(b)
―――――
10

Any reference to a global name var is syntactically translated to _ENV.var. You can use _ENV tables to adjust your current chunk’s environment

local function clone (t)
    local o = {}
    for k, v in pairs(t) do o[k] = v end
    return o
end

local function alter_inside (key)
    local _ENV = clone(_ENV)
    a = 5
    b = 6
    c = 7
    _ENV[key] = 11
    print(a, b, c)
end

alter_inside('a')
alter_inside('b')
alter_inside('c')
―――――
11      6       7
5       11      7
5       6       11


Next: String Functions