LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 417 min read

Variables & Data Types

Learn Lua's eight basic types, the difference between local and global variables, and Lua's rules for truthy and falsy values.

Introduction

Lua is dynamically typed with a deliberately small set of types — just eight of them cover everything the language works with. This lesson covers how variables work, what those eight types are, and two rules (nil vs false, and what counts as "truthy") that trip up nearly every newcomer at least once.

What You Will Learn
  • The difference between local and global variables, and why locals should be your default.
  • Lua's eight basic types and what type() reports for each.
  • How numbers, strings, and nil behave, including Lua's truthy/falsy rules.

Local vs Global Variables

Any variable assigned without the local keyword becomes global by default — visible from anywhere in the program, for the rest of its execution. Declaring a variable with local scopes it to the current block instead, which is almost always what you want.

x = 10 -- global (avoid this in real code)
local y = 20 -- local, scoped to this file/block
print(x, y)
Output

Click Run to see what this code prints.

Why Locals Are the Default You Should Reach For

Global variables in Lua are stored in a shared global table, which means a typo (assigning to totel instead of total) silently creates a brand-new global instead of raising an error — a classic source of hard-to-find bugs. Getting in the habit of typing local before every variable avoids this entirely.

The Eight Basic Types

Every value in Lua has exactly one of eight types, reported by the built-in type() function.

TypeExampleWhat It Represents
nilnilThe absence of a value — an uninitialized or explicitly cleared variable.
booleantrue, falseLogical true/false values.
number42, 3.14Both integers and floating-point numbers (as subtypes since Lua 5.3).
string"hello"Immutable sequences of characters.
functionfunction() endA first-class, callable value.
table{}Lua's single associative/array data structure — covered fully in lesson 5.
userdata(host-defined)Arbitrary data supplied by a host application embedding Lua (lesson 12).
thread(coroutine)A coroutine — Lua's built-in lightweight cooperative multitasking mechanism.
print(type(nil)) -- nil
print(type(true)) -- boolean
print(type(42)) -- number
print(type("hi")) -- string
print(type({})) -- table
print(type(print)) -- function
Output

Click Run to see what this code prints.

Numbers: Integers and Floats

Since Lua 5.3, the number type has two subtypes: integer and float. A literal without a decimal point (like 10) is an integer; one with a decimal point or written in scientific notation (like 10.0 or 1e2) is a float. Division with / always produces a float, while // performs floor (integer) division.

print(10 / 3) -- 3.3333333333333 (always a float)
print(10 // 3) -- 3 (floor division)
print(math.type(10)) -- integer
print(math.type(10.0)) -- float
Output

Click Run to see what this code prints.

Strings

Strings are immutable — any "modification" actually creates a new string. Lua supports single and double quotes interchangeably, plus a special long-bracket [[ ]] syntax for multi-line strings without escaping.

local greeting = "Hello"
local name = 'World'
local multiline = [[
This spans
multiple lines
with no escaping needed.
]]
print(greeting .. ", " .. name .. "!")
print(#greeting) -- the # operator returns a string's length
Output

Click Run to see what this code prints.

nil and Truthy/Falsy Rules

This is the single rule that surprises the most newcomers: in Lua, only nil and false are falsy. Every other value — including 0 and the empty string "" — is truthy. This is different from many other scripting languages, where 0 or "" are often falsy too.

if 0 then
print("0 is truthy in Lua!")
end
if "" then
print("empty string is truthy too!")
end
Output

Click Run to see what this code prints.

Common Mistakes

Avoid These Mistakes
  • Assuming 0 or "" is falsy, coming from a language like Python or JavaScript — in Lua, only nil and false are falsy.
  • Forgetting local, accidentally creating a global variable that leaks state across your whole program.
  • Using == to compare a number and a string that look the same ("5" == 5 is false — Lua does not auto-convert types in comparisons).

Best Practices

  • Always write local before a new variable unless you have a specific reason to make it global.
  • Use type() or math.type() when you're unsure what kind of value you're holding, especially while learning.
  • Remember Lua's truthy rule explicitly in conditionals — don't assume it matches another language you know.

Frequently Asked Questions

No — Lua is dynamically typed, so a variable's type is determined by whatever value it currently holds, and can change over its lifetime.

It represents data owned by the host application embedding Lua (like a game engine's object) rather than data created purely in Lua code — you'll rarely create it directly until lesson 12.

Since Lua 5.3, the / operator always performs float division regardless of its operands' subtype — use // specifically when you want integer (floor) division.

Key Takeaways

  • Variables are global by default unless declared with local — always prefer local.
  • Lua has eight basic types: nil, boolean, number, string, function, table, userdata, and thread.
  • Numbers have integer and float subtypes since Lua 5.3; / always returns a float, // performs floor division.
  • Only nil and false are falsy in Lua — everything else, including 0 and "", is truthy.

Summary

You now know Lua's full set of basic types, how local and global variables differ, and the truthy/falsy rule that catches nearly every newcomer once. Next, you'll meet the table — the single data structure that does the job of arrays, dictionaries, and objects all at once.

Next Lesson →

Tables: Lua's Core Data Structure