Variables, Types, and Control Flow
Lua is dynamically typed, a variable's type is decided by whatever value it currently holds, and can change at runtime.
Basic types
Lua
nil is Lua's "no value", it's what an undeclared variable holds, and it's the only value besides false that counts as falsy in a condition.
Control flow
Lua
- Every block (
if,for,while, functions) is closed with the keywordend, there are no curly braces in Lua. for i = 1, 5 do ... endis a numeric for loop: start, stop, and an optional step (for i = 10, 1, -1counts down).- Only
nilandfalseare falsy, everything else, including0and"", is truthy. This is a common gotcha for people coming from JavaScript or Python.
WARNING
Unlike most C-family languages, 0 is truthy in Lua. if 0 then print("yes") end prints "yes". Only nil and false are falsy.