0Pricing
Lua Academy · Lesson

Lua Data Types Overview

Explore nil, boolean, number, string, table, function, and userdata types.

Lua Data Types Overview is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome to Lua

Lua is a lightweight, fast, and embeddable scripting language used in game engines, web servers, and embedded systems. Before writing any real programs, you need to understand how Lua represents data.

Lua has 8 basic types: nil, boolean, number, string, table, function, userdata, and thread. Every value in Lua belongs to exactly one of these types.

-- Check the type of any value with type()
print(type(42))        -- number
print(type("hello"))   -- string
print(type(true))      -- boolean
print(type(nil))       -- nil

nil: The Absence of Value

nil is Lua's way of representing nothing or no value. It is the only value of its type. Variables that have never been assigned hold nil automatically.

Assigning nil to a global variable effectively deletes it. In tables, setting a key to nil removes that entry entirely.

-- Unassigned variables are nil
print(x)          -- nil

x = 10
print(x)          -- 10

x = nil
print(x)          -- nil (variable is gone)

boolean: true and false

The boolean type has exactly two values: true and false. Lua's truthiness rules are simple but important: only nil and false are falsy. Everything else — including the number 0 and the empty string "" — is truthy.

This differs from many languages where 0 or "" are considered false.

-- Lua truthiness examples
if 0 then print("0 is truthy!") end       -- prints!
if "" then print("empty str is truthy!") end -- prints!
if nil then print("nil") else print("nil is falsy") end
if false then print("false") else print("false is falsy") end

number: Integers and Floats

Lua 5.3+ has two subtypes of number: integer and float. Integers are exact 64-bit values; floats are IEEE 754 double-precision. In Lua 5.1/5.2, all numbers are doubles.

The math.type() function distinguishes between the two subtypes. Division with / always returns a float; // (floor division) returns an integer when both operands are integers.

print(type(10))       -- number
print(type(10.5))     -- number
print(math.type(10))  -- integer
print(math.type(10.5))-- float
print(10 / 3)         -- 3.3333...
print(10 // 3)        -- 3  (floor division)

string: Text in Lua

Strings in Lua are immutable sequences of bytes. You can delimit them with single quotes, double quotes, or long brackets [[...]] for multi-line strings. Long bracket strings ignore the first newline and preserve everything else literally.

Lua strings are interned, so identical strings share memory. The length operator # returns the byte count.

local s1 = 'single quotes'
local s2 = "double quotes"
local s3 = [[multi-line
long string]]

print(#s1)           -- 13
print(type(s1))      -- string
print(s1 .. " works") -- concatenation with ..

table: The Universal Container

The table type is Lua's only compound data structure, yet it's powerful enough to implement arrays, dictionaries, objects, sets, and more. Tables are associative arrays: they map keys to values, where keys can be any non-nil, non-NaN value.

Tables are created with curly braces {}. Array-style tables use integer keys starting from 1.

-- Array style
local fruits = {"apple", "banana", "cherry"}
print(fruits[1])   -- apple (1-indexed!)

-- Dictionary style
local person = {name = "Alice", age = 30}
print(person.name) -- Alice
print(person["age"]) -- 30

function: First-Class Values

Functions in Lua are first-class values of type function. They can be stored in variables, passed as arguments, and returned from other functions. This makes Lua naturally suited to functional programming patterns.

You can define functions using the function keyword or assign anonymous functions to variables. Both forms are equivalent.

-- Two equivalent ways to define a function
function greet(name)
  return "Hello, " .. name
end

local greet2 = function(name)
  return "Hello, " .. name
end

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

userdata and thread

userdata represents arbitrary C data managed by the host application. You cannot create userdata in pure Lua — it comes from C extensions or embedding environments like game engines. It allows C code to expose objects to Lua scripts.

thread represents coroutines — independent execution threads with their own stacks. We'll explore coroutines in a dedicated lesson. For now, know that coroutine.create() returns a value of type thread.

-- thread type via coroutine
local co = coroutine.create(function()
  print("coroutine body")
end)

print(type(co))  -- thread

-- userdata comes from C, not creatable in pure Lua
-- type(io.stdin) == "file" is a userdata variant

Using type() to Inspect Values

The built-in type() function always returns a string naming the type of its argument. This is useful for defensive programming and debugging. You can compare the result directly against the type name strings.

Note that type(nil) returns the string "nil", not the value nil itself. This lets you safely check if a variable holds nil without errors.

local values = {42, "hi", true, nil, {}, function() end}

-- Note: nil in table stops ipairs early
for i = 1, 3 do
  print(i, type(values[i]))
end
-- 1  number
-- 2  string
-- 3  boolean

Type Differences from Other Languages

If you come from JavaScript, Python, or Java, a few Lua type rules may surprise you. There is no separate integer type visible to type() — both integers and floats return "number". Arrays don't have their own type — they're just tables. Classes don't exist — objects are tables with metatables.

Understanding these differences prevents common bugs when moving between languages.

-- No separate array type
local arr = {1, 2, 3}
print(type(arr))      -- table (not array!)

-- No separate integer type visible
print(type(1))        -- number
print(type(1.0))      -- number

-- math.type distinguishes
print(math.type(1))   -- integer
print(math.type(1.0)) -- float

Quick Check

What does type(0) return in Lua?

Recap: Lua Data Types Overview

You've learned all 8 Lua types:

  • nil — absence of value; falsy
  • boolean — true/false; only nil and false are falsy
  • number — integer or float; use math.type() to distinguish
  • string — immutable byte sequences; use .. to concatenate
  • table — the universal container for arrays and dictionaries
  • function — first-class values storable in variables
  • userdata — C data exposed to Lua
  • thread — coroutine handles

Next: declaring variables — global vs local scope.

Frequently asked questions

Is the “Lua Data Types Overview” lesson free?

Yes — the full text of “Lua Data Types Overview” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Lua Data Types Overview”?

Explore nil, boolean, number, string, table, function, and userdata types. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Lua Data Types Overview” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lua Data Types Overview
  2. Declaring Variables in Lua
  3. Arithmetic and Relational Operators
  4. Type Coercion and Conversion
← Back to Lua Academy