Lua Variables & Data Types
Learn about variables, data types, and basic operations in Lua
Variables in Lua
Variables in Lua are used to store data values. Unlike many programming languages, Lua variables don't have types - only values do. This makes Lua very flexible.
-- Variable declaration and assignment
local name = "John"
local age = 25
local isActive = true
-- Printing variables
print(name)
print(age)
Local vs Global Variables
In Lua, variables can be either local or global. It's generally recommended to use local variables whenever possible for better performance and to avoid naming conflicts.
-- Global variable (not recommended)
playerName = "GlobalPlayer"
-- Local variable (preferred)
local enemyName = "LocalEnemy"
function testScope()
-- Can access global variables
print(playerName)
-- Can access local variables declared in the same scope
print(enemyName)
-- Local to the function
local functionLocal = "I only exist in this function"
print(functionLocal)
end
testScope()
-- This would error: print(functionLocal)
Data Types in Lua
Lua has eight basic types: nil, boolean, number, string, function, userdata, thread, and table.
Numbers
local integer = 42
local decimal = 3.14
local scientific = 1.25e6 -- 1,250,000
local hex = 0xFF -- 255 in decimal
Numbers in Lua are double precision floating point by default. There's no separate integer type.
Strings
local single = 'Single quotes'
local double = "Double quotes"
local multiline = [[
This is a
multiline string
]]
local concat = "Hello " .. "World" -- Concatenation
Strings are immutable sequences of characters. Use .. to concatenate strings.
Booleans
local isTrue = true
local isFalse = false
-- In conditionals, only false and nil are considered "falsy"
-- Everything else is "truthy" including 0 and empty strings
if isTrue then
print("This will execute")
end
Boolean values represent true or false. In Lua, only false and nil are considered falsy.
Tables
-- Array-like table
local colors = {"red", "green", "blue"}
-- Dictionary-like table
local person = {
name = "John",
age = 30,
isAdmin = true
}
-- Accessing values
print(colors[1]) -- "red" (Lua arrays are 1-indexed)
print(person.name) -- "John"
Tables are the only data structure in Lua and can implement arrays, dictionaries, sets, and more.
-- Checking types with type() function
local name = "Lua"
local age = 30
local scores = {95, 87, 92}
local isValid = true
print(type(name)) -- "string"
print(type(age)) -- "number"
print(type(scores)) -- "table"
print(type(isValid)) -- "boolean"
print(type(print)) -- "function"
print(type(nil)) -- "nil"
Operators in Lua
Lua supports a variety of operators for arithmetic, comparison, logical operations, and more.
Arithmetic Operators
local a = 10
local b = 3
print(a + b) -- Addition: 13
print(a - b) -- Subtraction: 7
print(a * b) -- Multiplication: 30
print(a / b) -- Division: 3.3333...
print(a % b) -- Modulo: 1
print(a ^ b) -- Exponentiation: 1000
Comparison Operators
local a = 10
local b = 3
print(a == b) -- Equal to: false
print(a ~= b) -- Not equal to: true
print(a > b) -- Greater than: true
print(a < b) -- Less than: false
print(a >= b) -- Greater than or equal to: true
print(a <= b) -- Less than or equal to: false
Logical Operators
local a = true
local b = false
print(a and b) -- Logical AND: false
print(a or b) -- Logical OR: true
print(not a) -- Logical NOT: false
-- Short-circuit evaluation
local x = nil
local y = x or "default" -- y will be "default"
Concatenation & Length
local str1 = "Hello"
local str2 = "World"
print(str1 .. " " .. str2) -- Concatenation: "Hello World"
local list = {10, 20, 30, 40}
print(#list) -- Length: 4
print(#str1) -- Length: 5
Coding Challenges
Test your understanding of Lua variables and data types with these interactive challenges.
Challenge 1: Variable Declaration & Types
Create three variables: a string named 'greeting' with value 'Hello, Lua!', a number named 'score' with value 95, and a boolean named 'isCompleted' with value true. Then print each variable.
Challenge 2: Working with Tables
Create a table named 'person' with keys 'name', 'age', and 'hobbies'. The 'hobbies' key should contain an array of at least two string values. Then print the person's name and their first hobby.
Challenge 3: Type Conversion
Convert the string '42' to a number, add 8 to it, and store the result in a variable called 'result'. Then convert that number back to a string and concatenate it with the text ' is the answer'.
Type Coercion in Lua
Lua will automatically convert between strings and numbers when needed:
-- String to number conversion
local numberString = "42"
local number = tonumber(numberString)
print(number + 8) -- 50
-- Number to string conversion
local num = 42
local str = tostring(num)
print(str .. " is the answer") -- "42 is the answer"
-- Automatic conversion in arithmetic operations
print("5" + 2) -- 7 (string "5" converted to number)
-- This would fail: print("hello" + 5)
Operations
Lua supports various arithmetic, comparison, and logical operations.
Arithmetic Operators
local a = 10
local b = 3
print(a + b) -- Addition: 13
print(a - b) -- Subtraction: 7
print(a * b) -- Multiplication: 30
print(a / b) -- Division: 3.3333...
print(a % b) -- Modulo: 1
print(a ^ b) -- Exponentiation: 1000
Comparison Operators
-- Equality: == (equal to) and ~= (not equal to)
print(5 == 5) -- true
print(5 ~= 5) -- false
-- Comparison operators
print(5 > 3) -- true
print(5 < 3) -- false
print(5 >= 5) -- true
print(5 <= 3) -- false
Logical Operators
-- and, or, not
print(true and false) -- false
print(true or false) -- true
print(not true) -- false
-- Short-circuit evaluation
local x = nil
local y = x or "default" -- y will be "default"