Lua Data Types
A data type is the kind of a value. Calling type() on a value gives its type as a word.
Four values, four type names
One print asks type() about a number, a string, a boolean, and nil. The four answers appear in order, split by tabs.
print(type(42), type("hello"), type(true), type(nil))nil prints as the word nil
The variable bonus holds nil, so it holds no value. Printing the variable shows nil, and its type is nil too.
local bonus = nil
print(bonus)
print(type(bonus))Whole and decimal numbers share one type
The values 42 and 2.0 print in different forms. type() calls both of them number.
print(42, 2.0)
print(type(42), type(2.0))Example
local name = "Kai"
local score = 85
local alive = true
local bonus = nil
print(name, type(name))
print(score, type(score))
print(alive, type(alive))
print(bonus, type(bonus))Key points
- Number: the type for all numbers, whole and decimal alike.
- String: the type for text written inside quotes.
- Boolean: the type for the two values true and false.
- Nil: the type for nil, which means no value.
- type(): type() gives the type of a value as a word.
In this example, four variables hold one value of each basic type. Each print shows the value and then the answer from type(). The last variable holds nil, so its value prints as nil and its type is nil.
Note
Lua has one number type, so whole numbers and decimal numbers share the name number.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does type(2.0) give?