TExcelTAD
Home › Lua › Lua Data Types

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.

lua · try it yourself
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:

lua · try it yourself
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:

lua · try it yourself
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") end
1	truthy
2	truthy
3	truthy
4	falsy
nil is falsy

Integers and floats

The number type has two subtypes. math.type tells them apart:

lua · try it yourself
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
lua · try it yourself
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.

lua · try it yourself
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 tonumber so that bad input is caught early. Note the space in 10 .. 20: without it, 10..20 is read as a malformed number.

Test yourself

Which values are false in a Lua condition?

  1. 0, "" and nil
  2. Only false
  3. Only nil and false
  4. nil, false and 0

What does type(type) return?

  1. "type"
  2. "function"
  3. "string"
  4. "builtin"

What does tonumber("12px") return?

  1. 12
  2. nil
  3. 0
  4. An error