Lua Operators
Operators combine values into new values. Lua's set is small, but a few of them look different from C-family languages: ~= for "not equal", .. for concatenation, # for length, and the words and, or, not.
Arithmetic
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * |
add, subtract, multiply | 5 * 3 |
15 |
/ |
division (float) | 9 / 2 |
4.5 |
// |
floor division | 9 // 2 |
4 |
% |
modulo | 9 % 2 |
1 |
^ |
power (float) | 2 ^ 10 |
1024.0 |
- |
unary minus | -5 |
-5 |
Lua has no ++, --, += or -=. Write count = count + 1.
Comparison
| Operator | Meaning |
|---|---|
== |
equal |
~= |
not equal |
< > <= >= |
ordering (numbers with numbers, strings with strings) |
print(3 == 3.0, 3 ~= 4, "a" == "a")
print(2 < 10, "2" < "10")
print({} == {}) -- two different tables
local t = {}
print(t == t) -- the same table
print(pcall(function() return 1 < "2" end))true true true
true false
false
true
false /app/output.s:6: attempt to compare number with string
Tables, functions and coroutines are compared by identity: two tables with the same contents are still different objects. Comparing a number with a string using < is an error (only == is allowed across types, and it is always false).
Logical operators
and, or and not work on any values, using Lua's truthiness (only nil and false are false). and and or short-circuit and return one of their operands, not necessarily a boolean:
| Expression | Returns |
|---|---|
a and b |
a if a is falsy, otherwise b |
a or b |
a if a is truthy, otherwise b |
not a |
true if a is falsy, otherwise false |
print(nil and 1, false and 1, 0 and "zero is truthy")
print(nil or "default", "set" or "default")
print(not nil, not 0)
-- idioms
local config = {}
local port = config.port or 8080 -- default value
local label = (5 > 3) and "big" or "small" -- ternary-like
print(port, label)nil false zero is truthy
default set
true false
8080 big
Note: The
cond and a or bidiom fails whenaisfalseornil:true and false or "x"gives"x". Use anifstatement in that case.
Concatenation and length
.. joins strings and numbers into a string. # returns the length of a string (in bytes) or of a sequence table:
local parts = { "a", "b", "c" }
print("v" .. 5 .. "." .. 4)
print(#"hello", #parts)
print("Total: " .. #parts * 2)v5.4
5 3
Total: 6
Precedence
From highest to lowest:
| Level | Operators |
|---|---|
| 1 | ^ (right-associative) |
| 2 | not # - ~ (unary) |
| 3 | * / // % |
| 4 | + - |
| 5 | .. (right-associative) |
| 6 | << >> |
| 7 | & |
| 8 | ~ (binary xor) |
| 9 | | |
| 10 | < > <= >= ~= == |
| 11 | and |
| 12 | or |
print(2 + 3 * 4, (2 + 3) * 4)
print(-2 ^ 2, (-2) ^ 2) -- ^ binds tighter than unary minus
print(2 ^ 3 ^ 2) -- right-associative: 2 ^ 9
print(1 + 2 .. "") -- arithmetic first, then ..
print(not 1 == 2) -- (not 1) == 214 20
-4.0 4.0
512.0
3
false
not 1 == 2 is a classic trap: not binds tighter than ==, so it is false == 2. Write not (1 == 2) or 1 ~= 2. When in doubt, add parentheses.
Test yourself
Which operator means "not equal" in Lua?
!=<>~==/=
What does nil or "guest" evaluate to?
niltrue"guest"false
What is -3 ^ 2?
9.0-9.09-6