Variables and Data Types
Master Lua's type system, scoping rules, strings, and numbers.
What You'll Learn
- Local vs global variable scoping
- Multiple assignment and variable swapping
- Number system and math library
- String operations and the # length operator
Local vs Global Variables
In Lua, every variable is global by default unless you explicitly declare it with local. This is the opposite of most modern languages, and it's Lua's biggest "gotcha" for beginners.
๐ฎ Real-World Analogy: Think of global variables like writing on a shared whiteboard โ anyone in any room can see and change it. Local variables are like writing in your private notebook โ only you (and your current block of code) can see them.
local score = 100 -- โ Good: scoped to this block health = 50 -- โ Bad: pollutes global namespace do local temp = "secret" print(temp) -- works fine end print(temp) -- nil! temp doesn't exist outside the block
โ ๏ธ Common Mistake
Typos in variable names silently create new global variables instead of throwing an error. If you write socre = 100 instead of score = 100, Lua won't complain โ it just creates a new global called socre.
Scoping & Strings
Understand local vs global variables and string operations.
// Lua Scoping Rules โ simulated in JavaScript
console.log("=== Local vs Global Variables ===");
console.log();
// In Lua:
// x = 10 -- GLOBAL (accessible everywhere โ avoid!)
// local y = 20 -- LOCAL (scoped to current block)
console.log("Without 'local' โ variable is GLOBAL");
console.log("With 'local' โ variable is LOCAL to its block");
console.log();
console.log("=== Block Scoping Example ===");
console.log(`
do
local secret = 42 -- only exists inside this 'do
...Numbers and the Math Library
Lua 5.3+ has two number sub-types: integers and floats. In earlier versions, all numbers were 64-bit floats. Lua automatically converts between them when needed.
local age = 25 -- integer local pi = 3.14159 -- float local big = 1e6 -- 1000000.0 (scientific notation) -- Floor division (integer result) print(7 // 2) -- 3 -- Float division (always a float) print(7 / 2) -- 3.5 -- Exponentiation print(2 ^ 10) -- 1024.0
Useful Math Functions
math.floor(3.7) -- 3 math.ceil(3.2) -- 4 math.abs(-42) -- 42 math.max(1, 5, 3) -- 5 math.min(1, 5, 3) -- 1 math.random(1, 10) -- random integer between 1 and 10 math.pi -- 3.14159265...
Numbers & Math
Explore Lua's number system and math library.
// Lua Numbers & Math โ simulated in JavaScript
console.log("=== Lua Number System ===");
console.log("Lua uses 64-bit floating-point for ALL numbers");
console.log();
console.log("Integer-like: 42");
console.log("Decimal: 3.14");
console.log("Scientific: 1e10 =", 1e10);
console.log("Hex literal: 0xFF =", 0xFF);
console.log();
console.log("=== Arithmetic Operators ===");
const ops = [
["10 + 3", "=", 10 + 3, "(addition)"],
["10 - 3", "=", 10 - 3, "(subtraction)"],
["10 *
...Strings In Depth
Strings in Lua are immutable sequences of bytes. Lua provides a rich string library for manipulation:
local s = "Hello, Lua!"
-- Length
print(#s) -- 11
-- Case
print(string.upper(s)) -- "HELLO, LUA!"
print(string.lower(s)) -- "hello, lua!"
-- Substring
print(string.sub(s, 1, 5)) -- "Hello"
-- Find & Replace
print(string.find(s, "Lua")) -- 8 10
print(string.gsub(s, "Lua", "World")) -- "Hello, World!"
-- Format (like printf)
print(string.format("Score: %d / %d", 85, 100)) -- "Score: 85 / 100"๐ก Pro Tip
Multi-line strings use double square brackets: [[...]]. They preserve all whitespace and newlines, perfect for templates and long text.
๐ Quick Reference
| Concept | Syntax |
|---|---|
| Local variable | local x = 10 |
| Multiple assign | local a, b = 1, 2 |
| Swap | a, b = b, a |
| String length | #"hello" |
| Concatenate | "a" .. "b" |
| Floor division | 7 // 2 |
๐ Lesson Complete!
You now understand Lua's variable scoping, data types, numbers, and strings. Next up: functions and Lua's most powerful feature โ tables!
Sign up for free to track which lessons you've completed and get learning reminders.