Data Types in Lua

Lua has 8 basic data types. Understanding these types is crucial for effective programming. Lua is dynamically typed, meaning variables don’t have fixed types—their type is determined by the value they hold.

The 8 Data Types

1. Nil

nil represents the absence of a value or a non-existent variable:

local empty_var = nil
print(type(empty_var))  -- nil

-- Uninitialized variables are nil
print(some_undefined_var)  -- nil

2. Boolean

Boolean values represent truth or false:

local is_ready = true
local is_finished = false

print(type(is_ready))    -- boolean
print(type(is_finished)) -- boolean

-- Booleans in conditions
if is_ready then
    print("Ready to proceed!")
end

3. Number

Numbers represent real numbers (integers and floating-point):

local integer = 42
local decimal = 3.14159
local scientific = 1.5e-3

print(type(integer))     -- number
print(type(decimal))     -- number
print(type(scientific))  -- number

-- Mathematical operations
print(10 + 5)    -- 15
print(10 - 3)    -- 7
print(10 * 4)    -- 40
print(10 / 2)    -- 5.0
print(10 % 3)    -- 1 (remainder)
print(2 ^ 3)     -- 8 (exponent)

4. String

Strings represent text and can be delimited by single quotes, double quotes, or double square brackets:

local single_quote = 'Hello'
local double_quote = "World"
local multiline = [[
This is a
multi-line string
that can span
multiple lines
]]

print(type(single_quote))  -- string

String concatenation:

local first = "Hello"
local last = "World"
local full = first .. " " .. last
print(full)  -- Hello World

String length:

local text = "Lua Programming"
print(#text)  -- 14 (length operator)

5. Table

Tables are Lua’s most powerful and flexible data structure. They can act as arrays, dictionaries, or both:

-- Array-like table
local fruits = {"apple", "banana", "orange"}
print(fruits[1])  -- apple (Lua arrays are 1-indexed)

-- Dictionary-like table
local person = {
    name = "Alice",
    age = 30,
    city = "New York"
}
print(person.name)  -- Alice
print(person["age"]) -- 30

-- Mixed table
local mixed = {
    "first",  -- array element
    name = "John",  -- key-value pair
    age = 25,
    "last"   -- array element
}

6. Function

Functions are first-class values in Lua—they can be stored in variables, passed as arguments, and returned from other functions:

-- Function as variable
local greet = function(name)
    return "Hello, " .. name
end

print(type(greet))  -- function
print(greet("Alice"))  -- Hello, Alice

-- Function can be stored in tables
local math_ops = {
    add = function(a, b) return a + b end,
    multiply = function(a, b) return a * b end
}

print(math_ops.add(5, 3))        -- 8
print(math_ops.multiply(4, 7))  -- 28

7. Userdata

Userdata allows C data to be stored in Lua variables. It’s typically used when interfacing with C libraries:

-- Example: File handle (this is a simplified example)
-- local file = io.open("test.txt", "r")
-- print(type(file))  -- userdata (in real usage)

8. Thread

Threads represent independent threads of execution. They’re used for coroutines in Lua:

-- Example with coroutine (simplified)
local co = coroutine.create(function()
    print("Coroutine started")
end)

print(type(co))  -- thread
coroutine.resume(co)

Type Checking and Conversion

Checking Types

Use the type() function to check a variable’s type:

local value = "Hello"
if type(value) == "string" then
    print("This is a string")
end

Type Conversion

Lua automatically converts between strings and numbers in arithmetic operations:

local num = "10"
local result = num + 5  -- Automatic conversion
print(result)  -- 15
print(type(result))  -- number

-- Explicit conversion
local str_num = "3.14"
local converted = tonumber(str_num)
print(converted + 2)  -- 5.14

-- Convert to string
local number = 42
local as_string = tostring(number)
print(type(as_string))  -- string

Type Coercion in Comparisons

-- Strict equality (no coercion)
print(10 == "10")  -- false

-- Type considerations in conditions
local value = "0"
if value then
    print("Non-nil and non-false values are truthy")
end

Next Steps

Now that you understand Lua’s data types, learn about operators to manipulate these values.

For detailed information about Lua types, see the Lua manual.

Last updated on