Chapter I ·
Language basics
/
Lesson 2 of 27
Locals & assignment
You'll learn: Declare locals, swap with one statement, and scope with
do…end.
Bare assignment creates a *global* — almost never what you want.
Use local for everything except top-level configuration.
Multiple assignment is first-class: a, b = 1, 2 (and
a, b = b, a swaps without a temp). do…end opens a fresh
scope; locals declared inside vanish after end.
Output
idle
Hit Run to execute.
Try it:
Drop the
local keyword from x, y and re-run. Lua quietly created a global — print _G.x to confirm.