Getting Started
4 snippetsBasic Lua syntax
Hello World
print("Hello, World!")Comments
-- Single line comment
--[[
Multi-line
comment
--]]Run Lua
lua script.lua
lua -e 'print("Hello")' -- One-linerSemicolons Optional
print("Hello")
print("World") -- No semicolons neededVariables & Data Types
6 snippetsDynamic typing
Variable Declaration
local x = 10 -- Local variable
y = 20 -- Global variable
local name = "Lua"Nil
local x = nil -- Nil/null value
if x == nil then
print("x is nil")
endNumbers
local int = 42
local float = 3.14
local exp = 1.5e-3Strings
local s1 = 'single quotes'
local s2 = "double quotes"
local s3 = [[multi-line
string]]Booleans
local flag = true
local active = false
-- Only nil and false are falsyType Checking
print(type(x)) -- "number", "string", etc.
print(type(nil)) -- "nil"Operators
5 snippetsArithmetic and logical operations
Arithmetic
local sum = a + b
local diff = a - b
local prod = a * b
local quot = a / b
local mod = a % b
local pow = a ^ b -- ExponentiationComparison
a == b -- Equal
a ~= b -- Not equal (note: ~=)
a > b -- Greater
a < b -- Less
a >= b -- Greater or equal
a <= b -- Less or equalLogical
a and b -- Logical AND
a or b -- Logical OR
not a -- Logical NOTString Concatenation
local full = first .. " " .. last
local msg = "Count: " .. tostring(42)Length Operator
#"hello" -- 5
#mytable -- Table lengthTired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Control Flow
4 snippetsConditional statements
If Statement
if age >= 18 then
print("Adult")
else
print("Minor")
endElseif
if score >= 90 then
grade = "A"
elseif score >= 80 then
grade = "B"
else
grade = "F"
endTruthy/Falsy
-- Only nil and false are falsy
if 0 then print("0 is truthy") end
if "" then print("Empty string is truthy") endTernary Alternative
-- Lua doesn't have ternary, use:
local result = condition and value1 or value2Loops
6 snippetsIteration constructs
While Loop
local i = 1
while i <= 10 do
print(i)
i = i + 1
endRepeat-Until
local i = 1
repeat
print(i)
i = i + 1
until i > 10Numeric For
for i = 1, 10 do
print(i)
end
for i = 10, 1, -1 do -- Step of -1
print(i)
endGeneric For (ipairs)
local arr = {10, 20, 30}
for i, v in ipairs(arr) do
print(i, v) -- 1 10, 2 20, 3 30
endGeneric For (pairs)
local t = {a=1, b=2}
for k, v in pairs(t) do
print(k, v) -- "a" 1, "b" 2
endBreak
for i = 1, 100 do
if i == 50 then break end
print(i)
endFunctions
5 snippetsFirst-class functions
Function Declaration
function greet(name)
print("Hello, " .. name)
end
greet("Alice")Return Values
function add(a, b)
return a + b
end
local sum = add(5, 3)Multiple Returns
function stats(a, b)
return a + b, a - b, a * b
end
local sum, diff, prod = stats(10, 5)Anonymous Functions
local square = function(x)
return x * x
end
print(square(5)) -- 25Variadic Functions
function sum(...)
local total = 0
for _, v in ipairs({...}) do
total = total + v
end
return total
end
print(sum(1, 2, 3, 4)) -- 10Tables
6 snippetsLua's primary data structure
Array-like Table
local arr = {10, 20, 30}
print(arr[1]) -- 10 (1-indexed!)Dictionary-like Table
local person = {
name = "John",
age = 30
}
print(person.name) -- "John"
print(person["age"]) -- 30Mixed Table
local t = {
10, 20, 30,
name = "mixed",
[5] = "five"
}Table Manipulation
table.insert(arr, 40) -- Add to end
table.insert(arr, 1, 5) -- Insert at index
table.remove(arr) -- Remove last
table.remove(arr, 1) -- Remove at indexTable Length
local arr = {10, 20, 30}
print(#arr) -- 3Iterate Table
for k, v in pairs(tbl) do
print(k, v)
endMetatables & OOP
3 snippetsAdvanced object-oriented features
Metatable Basics
local t = {}
local mt = {__index = {default = 10}}
setmetatable(t, mt)
print(t.default) -- 10Class Pattern
Dog = {}
Dog.__index = Dog
function Dog.new(name)
local self = setmetatable({}, Dog)
self.name = name
return self
end
function Dog:bark()
print(self.name .. " barks!")
end
local d = Dog.new("Buddy")
d:bark()Operator Overloading
local mt = {
__add = function(a, b)
return a.value + b.value
end
}
local a = setmetatable({value=5}, mt)
local b = setmetatable({value=3}, mt)
print((a + b)) -- 8