Cheatsheet: Lua
Last updated 2026-10-06
Variables and Types
Declare local variables (always prefer local over implicit global)
Check a value's type
Numbers: integers and floats are both "number"
Falsy values: only nil and false are falsy
Control Flow
if / elseif / else
while and repeat-until (repeat always runs at least once)
Numeric for loop: for i = start, stop, step do
Generic for loop with ipairs/pairs
Tables
Array-style table (1-indexed)
Map/dictionary-style table
Insert, remove, and nested tables
Strings
Concatenation and length
Common string library functions
string.format works like printf/sprintf
Splitting a string with gmatch (no built-in split())
Functions
Basic function and multiple return values
Variadic arguments with ...
Closures capture upvalues
Metatables (OOP)
A minimal class pattern with colon-method syntax
Operator overloading with metamethods
Coroutines
Create, resume, and yield
Error Handling
pcall: call a function and catch errors
assert: raise an error if a condition is false
Quick Recipes
Ternary-style expression (Lua has no ?: operator)
Default value for a possibly-nil argument
Simple module pattern (require)