Lua Cheatsheet

Basics

Use this Lua reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Lua Cheatsheet

Quick reference for standard Lua, targeting Lua 5.4. Version notes flag what differs in Lua 5.1–5.3 and LuaJIT (which implements 5.1 plus selected 5.2 features). Run any snippet in the Lua online compiler.

Running Lua

lua script.lua
lua -i script.lua      # run, then stay interactive
lua -e 'print(2 + 3)'  # execute one chunk
lua -v                 # print the version

A Lua file is a chunk executed from top to bottom. There is no required main function.

Comments

-- single-line comment

--[[
multiline comment
]]

Values and Types

local n = 42             -- number
local s = "hello"        -- string
local ok = true          -- boolean
local none = nil         -- no value
local t = {1, 2, 3}      -- table
local f = function() end -- function

Core types from type(x): nil, boolean, number, string, table, function, thread, userdata.

Integers and Floats (Lua 5.3+)

Since Lua 5.3 the number type has two subtypes. 1 is an integer, 1.0 and 1e2 are floats; math.type tells them apart.

math.type(1)        -- "integer"
math.type(1.0)      -- "float"
math.type("x")      -- nil (not a number)
print(1 == 1.0)     -- true, comparison is by value
print(3 / 2)        -- 1.5   `/` always produces a float
print(4 / 2)        -- 2.0   even when the result is exact
print(7 // 2)       -- 3     `//` keeps integer operands integer
print(7.0 // 2)     -- 3.0   float if either operand is a float
math.tointeger(3.0) -- 3
math.tointeger(3.5) -- nil (no exact integer value)

Integers are 64-bit and wrap on overflow. Converting a float with a fractional part where an integer is required (e.g. string.format("%d", 1.5) or t[1.5] arithmetic contexts) raises number has no integer representation. Lua 5.1/5.2 and LuaJIT have a single (double) number type.

Locals vs Globals

local x = 10   -- preferred: scoped to current block/chunk
x = 10         -- global assignment if no local x exists

Use local by default. Accidental globals are one of the most common Lua bugs.

Const and To-Be-Closed Variables (Lua 5.4)

Lua 5.4 adds attributes on local declarations.

local LIMIT <const> = 100        -- reassignment is a compile-time error
local f <close> = assert(io.open("data.txt"))
-- f is closed automatically when the block exits, even on error

<close> calls the value's __close metamethod at end of scope (file handles define one), giving deterministic cleanup for files, locks, and other resources.

Truthiness

Only false and nil are falsey. Everything else is truthy, including 0, "", and {}.

if 0 then print("runs") end
if "" then print("also runs") end

Operators

KindOperators
Arithmetic+ - * / // % ^ unary -
Comparison== ~= < > <= >=
Booleanand or not
Bitwise (5.3+)& | ~ (xor) << >> unary ~ (not)
Concatenation..
Length#
print(7 // 2)      -- 3, floor division in Lua 5.3+
print(2 ^ 10)      -- 1024.0 (`^` always returns a float)
print(5 & 3, 5 | 3, 5 ~ 3)  -- 1   7   6
print(1 << 4, ~0)  -- 16   -1
print("Lua " .. "rocks")
print(#"hello")    -- 5 bytes

Bitwise operands must be integers (or floats with exact integer values). On Lua 5.1/5.2 and LuaJIT use the bit/bit32 libraries instead.

Conversion

tonumber("42")      -- 42
tonumber("ff", 16)  -- 255
tonumber("cat")     -- nil
tostring(123)       -- "123"

Nil Defaults

local port = config.port or 8080
local count = (counts[word] or 0) + 1

Remember: false or default also returns the default. If false is a meaningful value, check x == nil explicitly.