100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Lua Types and Variables

How Lua's dynamic typing, variable scope, number/string types, and tables work, with the specifics that trip up newcomers.

FoundationsBeginner8 min readJul 10, 2026
Analogies

Lua Types and Variables

Lua is dynamically typed: variables themselves have no type, only the values they hold do. There are exactly eight basic types in Lua — nil, boolean, number, string, function, userdata, thread, and table — and the built-in type() function returns which one a given value currently is. A variable can hold a number at one point in a script and a string or table later, since type checking happens at runtime, not at compile time. This flexibility is powerful but places the burden of type discipline on the programmer rather than the compiler.

🏏

Cricket analogy: A Lua variable holding any type at runtime is like a versatile fielder such as Ravindra Jadeja being moved between slip, point, and the boundary within the same innings depending on the batsman — the 'role' (type) isn't fixed to the position, it's assigned situationally.

Variable Declaration and Scope

By default, any variable you assign in Lua — x = 10 — becomes a global variable, visible from anywhere in the program, including inside functions defined later, unless shadowed locally. Prefixing a declaration with local, as in local x = 10, restricts that variable's visibility to the enclosing block: a function body, an if statement, a loop, or the chunk (file) it's declared in. Because globals are stored in a shared table and are easy to overwrite accidentally, idiomatic Lua code declares almost everything with local, reserving true globals for values that genuinely need program-wide visibility, like configuration constants set once at startup.

🏏

Cricket analogy: A global Lua variable is like an announcement made over the stadium PA system at Eden Gardens — every player and spectator hears it, which is useful for the toss result but risky if two different match officials try to broadcast conflicting information.

Numbers and Strings

Since Lua 5.3, the number type has two subtypes: integer and float, distinguished internally but usually interchangeable in arithmetic — 10 is an integer, 10.0 is a float, and 10 / 2 always produces a float (5.0) because / is real division, while 10 // 2 uses floor division to produce an integer (5). Strings in Lua are immutable byte sequences; operations like string.upper(s) or s:upper() (using method-call syntax) always return a new string rather than modifying the original. Lua supports single-quoted, double-quoted, and long-bracket ([[ ... ]]) string literals, the last of which is useful for multi-line text without needing escape characters.

🏏

Cricket analogy: The distinction between 10 (integer) and 10.0 (float) in Lua is like the difference between a bowler's over count, always a whole number, and their economy rate, which is naturally a decimal like 6.25 — both describe the same over, but one is inherently fractional.

Tables — Lua's Only Data Structure

A table is Lua's single composite data structure, implemented internally as a hybrid of an array part and a hash part. When you write { "a", "b", "c" }, Lua stores it efficiently with integer keys 1, 2, 3 in the array part; when you write { name = "Aria", level = 12 }, it uses the hash part with string keys. You can mix both styles in the same table, and because table keys can be almost any value except nil, tables also serve as the basis for Lua's object-oriented programming patterns, where methods are just functions stored under string keys and looked up via the : syntax.

🏏

Cricket analogy: A Lua table storing both a numbered batting order and named player stats in one structure is like a scorecard that lists batsmen in order (1, 2, 3...) while also recording each player's individual stats by name — one document, two ways of organizing the same data.

lua
-- Numbers: integer vs float subtypes (Lua 5.3+)
local a = 10        -- integer
local b = 10.0       -- float
print(a / 2)          -- 5.0  (real division always yields a float)
print(a // 3)         -- 3    (floor division yields an integer here)

-- Strings are immutable
local s = "lua"
print(s:upper())      -- "LUA"
print(s)               -- "lua" (unchanged)

-- Tables: array part + hash part in one structure
local player = {
  "sword", "shield",       -- array part: player[1], player[2]
  name = "Aria",             -- hash part
  level = 12,
  heal = function(self, amount)
    self.level = self.level + amount
  end
}

print(type(player), player[1], player.name)
player:heal(3)
print(player.level)   -- 15

Use the local keyword by default for every variable you declare. Lua only makes a variable global if you omit local, and accidental globals — often caused by a typo in a variable name inside a function — are one of the most common sources of hard-to-trace bugs in Lua scripts.

  • Lua has eight basic types: nil, boolean, number, string, function, userdata, thread, and table.
  • Variables are dynamically typed — the type belongs to the value, not the variable name.
  • Declare variables with local by default; omitting local creates a global, visible everywhere.
  • Since Lua 5.3, numbers have integer and float subtypes; / always returns a float, // performs floor division.
  • Strings are immutable; methods like s:upper() return a new string rather than modifying the original.
  • Tables are Lua's only built-in composite structure, combining an array part and a hash part.
  • Object-oriented patterns in Lua are built on tables, with methods stored as functions under string keys.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#LuaStudyNotes#LuaTypesAndVariables#Lua#Types#Variables#Variable#StudyNotes#SkillVeris#ExamPrep