Lua Data Types
Lua is dynamically typed: variables don't have types, values do. There are exactly eight basic types, and the built-in function type returns the name of a value's type as a string.
print(type(nil))
print(type(true))
print(type(42))
print(type("hello"))
print(type({}))
print(type(print))
print(type(coroutine.create(function() end)))nil
boolean
number
string
table
function
thread
The eighth type, userdata, holds raw C data (a file handle, for example) and comes from C libraries or the host program. io.stdout is one:
print(type(io.stdout))userdata
The eight types
| Type | Values | Notes |
|---|---|---|
nil |
nil |
"no value"; the default for unset variables and missing table keys |
boolean |
true, false |
|
number |
42, 3.14, 0xFF, 1e6 |
two subtypes: integer and float |
string |
"text", 'text', [[text]] |
immutable byte sequences |
table |
{}, {1, 2}, {x = 1} |
the only data structure |
function |
print, function() end |
first-class values |
thread |
from coroutine.create |
coroutines, not OS threads |
userdata |
from C code | file handles, engine objects |
Truthiness: only nil and false are false
In conditions, Lua treats only nil and false as false. Everything else is true, including 0 and the empty string. This differs from Python, JavaScript and C:
local values = { 0, "", "false", false }
for i = 1, 4 do
if values[i] then
print(i, "truthy")
else
print(i, "falsy")
end
end
if nil then print("never") else print("nil is falsy") end1 truthy
2 truthy
3 truthy
4 falsy
nil is falsy
Integers and floats
The number type has two subtypes. math.type tells them apart:
print(math.type(10), math.type(10.0), math.type("10"))
print(10 == 10.0)
print(7 / 2, 7 // 2)integer float nil
true
3.5 3
/ always gives a float; // (floor division) keeps integers. The integers vs floats lesson explains the details.
Converting between types
| From → to | Function | Example |
|---|---|---|
| anything → string | tostring(v) |
tostring(12) → "12" |
| string → number | tonumber(s) |
tonumber("0x1F") → 31 |
| string in base b → number | tonumber(s, b) |
tonumber("101", 2) → 5 |
| float → integer | math.tointeger(x) |
math.tointeger(4.0) → 4 |
print(tostring(12) .. "!", tostring(nil), tostring(false))
print(tonumber("42"), tonumber("3.5"), tonumber("0x1F"), tonumber("101", 2))
print(tonumber("abc"), tonumber(""))
print(math.tointeger(4.0), math.tointeger(4.5))12! nil false
42 3.5 31 5
nil nil
4 nil
tonumber returns nil (not an error) when the string isn't a number, so you can test the result.
Automatic coercion
Lua converts between strings and numbers in some operations: arithmetic on numeric strings gives a number, and .. turns numbers into strings. Comparisons do not coerce: 10 == "10" is false.
print("10" + 5)
print(10 .. 20)
print(10 == "10")15
1020
false
Tip: Don't rely on string-to-number coercion in arithmetic; convert explicitly with
tonumberso that bad input is caught early. Note the space in10 .. 20: without it,10..20is read as a malformed number.
Test yourself
Which values are false in a Lua condition?
0,""andnil- Only
false - Only
nilandfalse nil,falseand0
What does type(type) return?
"type""function""string""builtin"
What does tonumber("12px") return?
12nil0- An error