Skip to content

Lua Data Types

Lua is a dynamically typed language. This means variables themselves don’t have fixed types; instead, types are associated with the values they hold. Values can be stored in variables, passed as arguments to functions, and returned as results.

Lua has eight fundamental data types for values:

Value TypeDescription
nilRepresents the absence of a useful value. It has only one possible value: nil. Variables not explicitly initialized default to nil. In conditional contexts, nil evaluates to false.
booleanIncludes the values true and false. Used for conditional logic. Note: In Lua, only false and nil are considered falsy; all other values (including 0 and empty strings) are truthy.
numberRepresents numeric values. Prior to Lua 5.3, all numbers were double-precision floating-point numbers. Lua 5.3 introduced an integer subtype, but type() still returns “number” for both integers and floats. Lua handles conversions automatically where appropriate.
stringRepresents immutable sequences of characters. Strings can be enclosed in single quotes ('...'), double quotes ("..."), or long brackets ([[...]] for multi-line strings).
functionRepresents functions. Functions are first-class citizens in Lua, meaning they can be stored in variables, passed as arguments, and returned as results. They can be written in Lua or C.
userdataRepresents arbitrary C data. Allows C code to make custom data types available to Lua. There are two types: full userdata (Lua manages its memory) and light userdata (just a C pointer).
threadRepresents independent threads of execution, used to implement coroutines. Coroutines allow for cooperative multitasking, not preemptive OS-level threads.
tableThe sole data structuring mechanism in Lua. Tables are associative arrays (also known as maps or dictionaries) that can be used to represent ordinary arrays, sets, records, graphs, trees, objects, and more. They can hold any value except nil as keys or values (using nil as a key or assigning nil to a key effectively removes the key-value pair).

Lua provides a built-in function type() that returns a string describing the type of a given value. Here are some examples:

print(type("What is my type?")) --> string
print(type(10)) --> number
print(type(3.14)) --> number
print(type(true)) --> boolean
print(type(print)) --> function (print is a global function)
print(type(type)) --> function (type is also a function)
print(type(nil)) --> nil
local myTable = {}
print(type(myTable)) --> table
local co = coroutine.create(function() end)
print(type(co)) --> thread
-- For an undefined variable, its value is nil
print(type(undefinedVariable)) --> nil (assuming undefinedVariable has not been assigned a value)
-- The type of the result of type(X) is always string
print(type(type(123))) --> string (because type(123) is "number", and type("number") is "string")

Output of the above code:

string
number
number
boolean
function
function
nil
table
thread
nil
string

Remember that variables not explicitly initialized default to nil. A common pitfall for beginners is assuming 0 or an empty string "" might be false in conditions; in Lua, they are both considered true. Only false and nil evaluate to false in boolean contexts. Understanding these types is crucial for effective Lua programming.