Skip to content

Lua Operators

An operator is a symbol that instructs the Lua interpreter to perform a specific mathematical, relational, or logical operation. Lua offers a rich set of built-in operators, which can be categorized as follows:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Miscellaneous Operators (Concatenation, Length)

This tutorial will explain each type of operator with examples.

The following table lists the arithmetic operators supported by Lua. Assume local a = 10 and local b = 20:

Example Code:

local a = 10
local b = 20
print("a + b = ", a + b) -- Output: 30
print("a - b = ", a - b) -- Output: -10
print("a * b = ", a * b) -- Output: 200
print("b / a = ", b / a) -- Output: 2.0 (floating point division)
print("b % a = ", b % a) -- Output: 0 (remainder of division)
print("a ^ 2 = ", a ^ 2) -- Output: 100 (exponentiation)
print("-a = ", -a) -- Output: -10 (unary minus, negation)
-- Lua 5.3+ also introduced integer division //
-- print("7 // 2 = ", 7 // 2) -- Output: 3 (if using Lua 5.3+)
OperatorDescriptionExample (a=10, b=20)
+Addition: Adds two operands.a + b results in 30
-Subtraction: Subtracts the second operand from the first.a - b results in -10
*Multiplication: Multiplies both operands.a * b results in 200
/Division: Divides the numerator by the denominator. Always results in a float.b / a results in 2.0
%Modulo: Remainder of an integer division.b % a results in 0
^Exponentiation: Raises the first operand to the power of the second operand.a ^ 2 results in 100
-Unary Minus: Negates its operand.-a results in -10

Relational operators compare two values and return a boolean result (true or false). Assume local a = 10 and local b = 20:

Example Code:

local a = 10
local b = 20
print("a == b is", a == b) -- Output: a == b is false
print("a ~= b is", a ~= b) -- Output: a ~= b is true
print("a > b is", a > b) -- Output: a > b is false
print("a < b is", a < b) -- Output: a < b is true
print("a >= b is", a >= b) -- Output: a >= b is false
print("a <= b is", a <= b) -- Output: a <= b is true
OperatorDescriptionExample (a=10, b=20)
==Equality: Checks if the values of two operands are equal.(a == b) is false.
~=Inequality: Checks if the values of two operands are not equal.(a ~= b) is true.
>Greater than: Checks if the value of the left operand is greater than the value of the right operand.(a > b) is false.
<Less than: Checks if the value of the left operand is less than the value of the right operand.(a < b) is true.
>=Greater than or equal to: Checks if the value of the left operand is greater than or equal to the value of the right operand.(a >= b) is false.
<=Less than or equal to: Checks if the value of the left operand is less than or equal to the value of the right operand.(a <= b) is true.

Logical operators are used to combine or modify boolean expressions. In Lua, false and nil are considered falsy, while all other values are truthy. Logical operators and and or exhibit short-circuiting behavior and return one of their operands, not necessarily a boolean.

Assume local x = true, local y = false, local name = "Lua", local version = nil:

Example Code:
local x = true
local y = false
print("x and y is", x and y) -- Output: x and y is false (y is returned)
print("x or y is", x or y) -- Output: x or y is true (x is returned)
print("not x is", not x) -- Output: not x is false
local name = "Lua"
local version = nil
-- Short-circuiting behavior:
-- 'and' returns its first operand if it's false or nil, otherwise returns its second operand.
print("version and name:", version and name) -- Output: version and name: nil (version is nil)
print("name and version:", name and version) -- Output: name and version: nil (version is nil, name is truthy)
print("name and 'Rocks!':", name and "Rocks!") -- Output: name and 'Rocks!': Rocks!
-- 'or' returns its first operand if it's not false or nil, otherwise returns its second operand.
print("version or '5.4':", version or "5.4") -- Output: version or '5.4': 5.4 (version is nil)
print("name or 'Default':", name or "Default") -- Output: name or 'Default': Lua (name is truthy)
OperatorDescriptionExample (x=true, y=false)
andLogical AND: If the first operand is falsy (false or nil), returns it. Otherwise, returns the second operand.(x and y) results in false (value of y).
orLogical OR: If the first operand is truthy (not false and not nil), returns it. Otherwise, returns the second operand.(x or y) results in true (value of x).
notLogical NOT: Reverses the logical state of its operand. Returns true if the operand is falsy, false if truthy.not x results in false.

Lua includes operators for string concatenation and determining the length of strings or sequence-like tables.

Example Code:
local greeting = "Hello"
local target = "World"
local message = greeting .. " " .. target .. "!" -- Concatenation
print(message) -- Output: Hello World!
local str_len = #message
print("Length of message:", str_len) -- Output: Length of message: 12
local my_array = {10, 20, 30}
local arr_len = #my_array
print("Length of array:", arr_len) -- Output: Length of array: 3
OperatorDescriptionExample
..Concatenation: Joins two strings."Hello " .. "World" results in "Hello World".
#Length Operator (Unary): Returns the length of a string (in bytes) or a sequence table (number of elements from index 1 up to the first nil value).#"Hello" results in 5. #{1,2,3} results in 3.

Operator precedence defines the order in which operations are performed in an expression. Operators with higher precedence are evaluated before operators with lower precedence. For example, in x = 7 + 3 * 2, multiplication (*) has higher precedence than addition (+), so 3 * 2 is evaluated first (6), then 7 + 6 (13).

The following table lists operators from highest to lowest precedence. Most binary operators are left-associative, but exponentiation (^) and concatenation (..) are right-associative. Unary operators (not, #, unary -) are also right-associative.

PrecedenceOperatorAssociativity
Highest^Right to left
not # - (unary)Right to left
* / %Left to right
+ - (binary)Left to right
..Right to left
< > <= >= ~= ==Left to right (Note: Not chainable like a < b < c)
andLeft to right
LowestorLeft to right

You can use parentheses () to override the default precedence and force parts of an expression to be evaluated in a specific order.

Further Resources: For a comprehensive understanding of Lua operators and their nuances, consult the official Lua Reference Manual.