Variables in Lua

Variables are fundamental building blocks in programming. They act as containers that store data values you can use, modify, and reference throughout your program.

Creating Variables

In Lua, you don’t need to declare variable types. Just assign a value:

-- Simple variable assignment
name = "Alice"
age = 25
height = 5.6
is_student = true

print(name, age, height, is_student)
-- Output: Alice	25	5.6	true

Variable Naming Rules

Follow these rules when naming variables:

  • Start with a letter or underscore
  • Can contain letters, numbers, and underscores
  • Case-sensitive (name, Name, and NAME are different)
  • Avoid Lua keywords (like and, or, if, then, else)
-- Good variable names
first_name = "John"
totalScore = 95
is_admin = true
_privateVar = "secret"

-- Bad variable names (will cause errors)
-- 2ndplace = "second"  -- Starts with number
-- user-name = "bob"    -- Contains hyphen
-- local = "value"      -- Uses a keyword

Local vs Global Variables

Global Variables (default)

Variables are global by default, accessible from anywhere in your program:

-- Global variable
global_var = "I'm global everywhere"

function test_function()
    print(global_var)  -- Can access global_var
end

test_function()  -- Works fine

Local Variables (recommended)

Use local to limit variable scope and improve performance:

-- Local variable
local local_var = "I'm local here"

function test_function()
    local inner_local = "I'm local to this function"
    print(local_var)  -- Can access outer local
    print(inner_local)
end

test_function()
-- print(inner_local)  -- Error: variable not accessible here

Multiple Assignment

Lua allows assigning multiple values to multiple variables in one line:

-- Multiple assignment
name, age, city = "Bob", 30, "New York"

print(name, age, city)  -- Bob	30	New York

-- Swapping values without a temporary variable
a, b = 10, 20
print(a, b)  -- 10	20
a, b = b, a
print(a, b)  -- 20	10

Nil Values

nil represents the absence of a value:

empty_var = nil
print(empty_var)  -- nil

-- Checking if a variable exists
if my_var == nil then
    print("Variable doesn't exist or is nil")
end

Variable Types

Lua automatically handles different data types:

-- Different types of variables
text = "Hello"              -- string
number = 42                 -- number
decimal = 3.14              -- number
boolean = true              -- boolean
nothing = nil               -- nil

-- Variables can change types dynamically
value = 100
print(type(value))  -- number
value = "now I'm text"
print(type(value))  -- string

Best Practices

  1. Use local variables whenever possible for better performance
  2. Choose descriptive names that explain what the variable contains
  3. Use consistent naming conventions (camelCase or snake_case)
  4. Initialize variables before using them
-- Good practices
local user_name = "Alice"
local is_logged_in = false
local max_attempts = 3

-- Initialize local variables at the top
local score = 0
local level = 1
local health = 100

Next Steps

Now that you understand variables, learn about Lua’s data types to see what kinds of values you can store.

For more advanced variable concepts, check the Lua manual.

Last updated on