Lua Syntax Guide — Variables, Types, Operators, and Comments
In this tutorial, you will learn about Lua Syntax Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Lua syntax is minimal and clean -- variables are dynamically typed, semicolons are optional, and comments use double hyphens, making it approachable for beginners and embedded scripting.
What You'll Learn
- Variable declaration with local and global scope
- Lua's eight basic types
- Arithmetic, relational, and logical operators
- Comment syntax and documentation conventions
Why It Matters
Understanding Lua's syntax is the foundation for everything else. Lua's dynamic typing means fewer type annotations but requires attention to nil values. The minimal syntax reduces boilerplate but makes understanding the type system essential for writing correct code.
Real-World Use
Game developers write thousands of lines of Lua for Roblox or WoW addons. Configuration files for Redis and Nginx use Lua syntax. Every Lua script, from a simple calculator to a complex game mod, uses these same syntactic building blocks.
flowchart LR
A["Syntax"] --> B["Variables"]
B --> C["Types"]
C --> D["Operators"]
D --> E["Tables"]
A:::current --> B
style A fill:#2563eb,stroke:#2563eb,color:#fff
style B fill:#dbeafe,stroke:#2563eb,color:#1e40af
style C fill:#dbeafe,stroke:#2563eb,color:#1e40af
style D fill:#dbeafe,stroke:#2563eb,color:#1e40af
style E fill:#f1f5f9,stroke:#94a3b8,color:#64748b
Variables
Declaration
-- Global variable (no keyword)
name = "Alice"
-- Local variable (recommended)
local age = 30
-- Multiple assignment
local x, y, z = 1, 2, 3
-- Swap values using multiple assignment
local a, b = 10, 20
a, b = b, a
print(a, b) --> 20 10
Variables are global by default. Always use local unless you specifically need a global.
Naming Rules
- Letters, digits, and underscores
- Must start with a letter or underscore
- Case-sensitive:
Nameandnameare different - Reserved keywords cannot be used:
and,break,do,else,elseif,end,false,for,function,goto,if,in,local,nil,not,or,repeat,return,then,true,until,while
Data Types
Lua has eight basic types:
print(type(nil)) --> nil
print(type(true)) --> boolean
print(type(42)) --> number
print(type("hello")) --> string
print(type({})) --> table
print(type(print)) --> function
print(type(coroutine.create(function() end))) --> thread
print(type(newproxy())) --> userdata
nil
Represents the absence of a value. Any undeclared variable is nil.
local x -- x is nil
print(x) --> nil
x = 42
print(x) --> 42
x = nil
print(x) --> nil
boolean
true and false. In conditions, only false and nil are falsy -- everything else is truthy.
local is_active = true
local is_done = false
-- Falsy values: false and nil
if nil then
print("never runs")
end
if false then
print("never runs")
end
if 0 then
print("runs -- 0 is truthy in Lua")
end
if "" then
print("runs -- empty string is truthy")
end
number
Lua uses double-precision floating point by default (64-bit). Lua 5.3+ also has 64-bit integers.
local integer = 42
local float = 3.14
local negative = -10
local hex = 0xFF --> 255
local scientific = 1.5e10
-- Division always returns float
print(10 / 3) --> 3.3333333333333
-- Floor division (Lua 5.3+)
print(10 // 3) --> 3
string
Strings are immutable sequences of bytes. Can be single, double, or long bracket quoted.
local s1 = 'single quotes'
local s2 = "double quotes"
local s3 = [[multi-line
strings with
no escaping]]
-- Concatenation uses ..
print("Hello" .. " " .. "World") --> Hello World
-- Length operator
print(#"hello") --> 5
-- Strings are 1-indexed for byte access
local s = "Lua"
print(s:sub(1, 2)) --> Lu
Operators
Arithmetic
print(10 + 5) --> 15
print(10 - 5) --> 5
print(10 * 5) --> 50
print(10 / 3) --> 3.3333
print(10 // 3) --> 3 (floor division)
print(10 % 3) --> 1 (modulo)
print(10 ^ 2) --> 100 (exponentiation)
print(-10) --> -10 (unary minus)
Relational
print(5 == 5) --> true
print(5 ~= 5) --> false (not equal -- uses ~= not !=)
print(5 < 10) --> true
print(5 > 10) --> false
print(5 <= 5) --> true
print(5 >= 6) --> false
Logical
print(true and false) --> false
print(true or false) --> true
print(not true) --> false
-- Short-circuit evaluation
local x = nil or 42
print(x) --> 42 (nil is falsy, so 42 is used)
local y = "hello" or "world"
print(y) --> hello (short-circuits)
Concatenation and Length
-- .. for string concatenation
print("Lua" .. " " .. "rocks") --> Lua rocks
-- # for length
print(#"hello") --> 5
print(#{"a", "b"}) --> 2
Comments
-- Single line comment
--[[
Multi-line comment
--]]
--[[
Multi-line comment with -- in it
--]]
-- Short multi-line for debugging
local x = 1
--[=[
local x = 2 -- this line is commented out
--]=]
print(x) --> 1
Common Mistakes
1. Using != instead of ~=
Lua uses ~= for not equal, not !=. Using != causes a syntax error.
2. Forgetting local
Without local, variables pollute the global namespace. In large projects, this causes collisions and hard-to-find bugs.
3. Assuming 0 or empty string is falsy
Only false and nil are falsy in Lua. 0, "", and {} are all truthy. This differs from Python, JavaScript, and C.
4. Confusing .. with +
String concatenation uses .., not +. Using + on strings tries numeric addition.
5. Using \ for string escaping
Lua uses \ for escape sequences in single/double quoted strings, but the escape sequences differ from C. For example, \a is bell, \v is vertical tab.
6. Forgetting then in if statements
if condition then -- then is required. Missing it causes a syntax error.
Practice Questions
1. What are Lua's eight basic types? nil, boolean, number, string, table, function, thread, userdata.
2. What values are falsy in Lua?
Only false and nil. Everything else is truthy, including 0 and empty strings.
3. How do you declare a local variable in Lua?
Use the local keyword: local x = 42.
4. What operator does Lua use for not equal?
~= (tilde equals). The != operator does not exist in Lua.
5. How do you concatenate strings in Lua?
Using the .. operator: "Hello" .. " " .. "World".
Challenge: Write a Lua script that declares three local variables (name, age, is_student), prints them using concatenation, and demonstrates the difference between == and ~=.
FAQ
{{< faq question="Does Lua use semicolons?" >}} Semicolons are optional in Lua. They are only needed to separate two statements on the same line. Most Lua code omits semicolons entirely. {{< /faq >}}
{{< faq question="Is Lua statically or dynamically typed?" >}} Lua is dynamically typed. Variables don't have types -- values do. A variable can hold a number in one line and a string in the next. {{< /faq >}}
{{< faq question="What is the difference between nil and 0?" >}}
nil represents "no value." 0 is the number zero. In Lua, 0 is truthy while nil is falsy. They are completely different types (nil vs number).
{{< /faq >}}
{{< faq question="Does Lua have a null coalescing operator?" >}}
Lua doesn't have a dedicated ?? operator like C#, but x = a or b works as a null coalescing equivalent -- it assigns b if a is nil or false.
{{< /faq >}}
{{< faq question="Can I use += in Lua?" >}}
No. Lua doesn't have compound assignment operators. Use x = x + 1 instead of x += 1.
{{< /faq >}}
Try It Yourself
local name = "Lua Learner"
local age = 25
local is_fun = true
print("Name: " .. name)
print("Age: " .. age)
print("Is fun: " .. tostring(is_fun))
print("Type of name: " .. type(name))
print("Type of age: " .. type(age))
print("Type of is_fun: " .. type(is_fun))
Expected output:
Name: Lua Learner
Age: 25
Is fun: true
Type of name: string
Type of age: number
Type of is_fun: boolean
What's Next
Now that you understand variables and types, dive into Lua's most important data structure -- tables.
| Topic | Description | Link |
|---|---|---|
| Tables | Lua's universal data structure | {{< ref "04-tables" >}} |
| Functions | First-class functions and closures | {{< ref "05-functions" >}} |
| JavaScript | Compare Lua types with JavaScript | JavaScript |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro