Assert a condition in Lua with assert. Check types, validate function returns, and when not to use assert for user-facing errors.
Reference
Core Functions
Built-in functions available in every Lua program.
- assert
- collectgarbage
Control Lua's garbage collector: run cycles, tune pause and step multiplier, switch between incremental and generational modes, and query memory use.
- dofile: Open and Execute a Lua File
The dofile function in Lua 5.4 opens and executes a file as a chunk, returning all values the chunk returns. Errors propagate to the caller.
- error
Raise a runtime error in Lua. Stop execution, propagate errors up the call stack, and use the level parameter to blame the right caller.
- getmetatable
getmetatable returns the metatable of a Lua value, or its __metatable guard value when one is set. Works on tables, strings, userdata, and every other type.
- ipairs
Iterate over sequential Lua tables with ipairs. This iterator traverses array-style entries in order, stopping at the first nil gap.
- load
The Lua load() function compiles a chunk from a string or reader function without running it, returning the compiled function or a syntax error.
- loadfile
Load a Lua chunk from a file and return it as a function, without executing it. Supports text or binary mode and a custom _ENV.
- next
Traverse Lua tables in arbitrary order with next(). Returns the next key-value pair or nil when the table is empty.
- pairs
Iterate over key-value pairs in Lua tables with pairs. Unlike ipairs, pairs iterates all entries including non-sequential keys and handles nil values correctly.
- pcall
Call a function in protected mode. If the function raises an error, pcall catches it and returns false with the error message instead of propagating the error.
- print()
Output one or more values to the console. print is the simplest way to inspect values during development and debugging in Lua.
- rawequal
rawequal tests whether two Lua values are the same object in memory without invoking __eq metamethods. Use for identity checks in comparison and table-key code.
- rawget
rawget() retrieves a table value directly, bypassing the __index metamethod to give you the actual stored data without metatable fallbacks.
- rawlen
The rawlen function returns the length of a table or string without invoking __len metamethods, giving you the raw sequence length directly from the value.
- rawset
rawset() writes a value directly into a table, bypassing the __newindex metamethod so assignments hit raw storage without custom handlers.
- require
The Lua `require` function loads a module from the file system, a C library, or the package.preload table, caching the result in package.loaded for reuse.
- select
Select elements from a variadic argument list. Get all arguments after a position, or count the total arguments with the '#' selector.
- setmetatable
The setmetatable function assigns a metatable to a table, enabling custom behavior for operators, indexing, and function calls through metamethods.
- table.unpack
Lua 5.4's table.unpack returns list elements as multiple values, the standard way to spread a table into a function call or multi-assignment.
- tonumber
The tonumber() function converts strings and numbers to numeric values in Lua, supporting base 2-36 parsing for binary, hex, and octal input.
- tostring
The tostring() function converts any Lua value to a string — essential for debugging, logging, and building strings with concatenation of mixed types.
- type()
Return the type name of any value as a string. Use type() to check what kind of value you're working with at runtime.
- xpcall
xpcall calls a function with a custom error handler. The handler gets the raw error value, not a string. Essential for logging and structured error handling.