Statements and chunks
A Lua file is a chunk, a list of statements that need no semicolons or special ending.
In this page:
Statements and chunks
A chunk is any sequence of statements and is the unit Lua compiles and runs, either a file or a string. Statements are separated by whitespace, and semicolons are optional. The main chunk is itself a function that receives the command-line arguments as varargs.
Note:
Wrap the code in do ... end when you want a block with its own local variables.
Example: Statements and chunks
local a = 1; local b = 2
print(a + b)
do
local hidden = "inside block"
print(hidden)
end
print(select("#", ...))
print(load("return 6 * 7")())
-- Output:
-- 3
-- inside block
-- 0
-- 42
Login to try C/C++/Java/PHP code in the editor
Common Mistakes
- Putting a semicolon at the end of every line out of habit and thinking it is required
- Starting a line with a parenthesis and accidentally continuing the previous statement
- Forgetting end after a block
Chapter Summary
- A chunk is a sequence of statements
- Semicolons are optional
- The main chunk receives arguments as ...
- do ... end creates an explicit block
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: