Operators in Lua
Learn arithmetic, relational, logical, string, and bitwise operators in Lua with runnable Docker examples
Operators are the verbs of a programming language—they perform the work of combining, comparing, and transforming values. Lua, true to its minimalist philosophy, keeps its operator set small but expressive. Most operators look familiar to programmers coming from C or Python, but Lua has a few distinctive choices that catch newcomers off guard.
As a dynamically typed scripting language, Lua resolves operator behavior at runtime based on the operand types. This means the same + can work on integers, floats, or even tables (via metamethods). Where Lua diverges sharply from the C family is in its choice of .. for string concatenation, ~= for inequality, and the use of English keywords and, or, not for logical operations.
This tutorial covers Lua’s full operator set: arithmetic, relational, logical, string, length, and the bitwise operators introduced in Lua 5.3. You’ll also see how Lua 5.3+ distinguishes integer and float arithmetic—an important detail when porting code from older Lua versions.
Arithmetic Operators
Lua provides the standard arithmetic operators plus floor division and exponentiation. Since Lua 5.3, integers and floats are distinct subtypes of the number type, and operator behavior subtly depends on which you use.
Create a file named arithmetic.lua:
| |
Relational and Logical Operators
Lua uses ~= for “not equal” instead of !=. The logical operators are spelled out as words. A critical detail: in Lua, only nil and false are falsy—everything else (including 0 and the empty string "") is truthy.
Create a file named logical.lua:
| |
String and Length Operators
The .. operator concatenates strings. The # unary operator returns the length of a string or the number of elements in a sequence (array-like table). Numbers passed to .. are automatically converted to strings.
Create a file named strings.lua:
| |
Bitwise Operators and Precedence
Lua 5.3 introduced bitwise operators that work on integers. They use symbols (&, |, ~, <<, >>) that may clash visually with logical operators in other languages—remember that Lua’s logical operators are words, so symbols are always bitwise.
Operator precedence in Lua follows familiar mathematical conventions: ^ binds tightest, then unary operators, then * / // %, then + -, then .., then comparisons, then and, then or. Note that both ^ and .. are right-associative.
Create a file named bitwise_precedence.lua:
| |
Running with Docker
Pull the image once, then run each example:
| |
Expected Output
Output from arithmetic.lua:
a + b = 22
a - b = 12
a * b = 85
a / b = 3.4
a // b = 3
a % b = 2
a ^ b = 1419857.0
-a = -17
Type of 10 / 2 : float
Type of 10 // 2 : integer
Type of 2 ^ 3 : float
Output from logical.lua:
x == y : false
x ~= y : true
x < y : true
x > y : false
x <= y : true
x >= y : false
true and 'hello' : hello
nil and 'hello' : nil
false or 'default' : default
'first' or 'second' : first
display = Anonymous
0 is truthy : yes
'' is truthy : yes
Output from strings.lua:
Hello, Lua!
Lua version: 5.4
length of 'Hello' = 5
length of message = 11
number of fruits = 4
parts = abcd
Output from bitwise_precedence.lua:
a & b = 0x00
a | b = 0xFF
a ~ b = 0xFF
~a = 0xF
1 << 4 = 0x10
a >> 4 = 0x0F
2 + 3 * 4 = 14
2 ^ 3 ^ 2 = 512.0
-2 ^ 2 = -4.0
'x' .. 1 + 2 = x3
n after n = n + 1 : 11
Key Concepts
..is concatenation, not+— Lua reserves+for numeric addition; using it on strings is an error unless they convert to numbers.~=means “not equal” —!=is not valid Lua syntax. The~symbol pulls double duty as bitwise XOR (binary) and bitwise NOT (unary).- Logical operators short-circuit and return values —
a and breturnsaifais falsy, otherwiseb. This enables the idiomaticvalue or defaultpattern. - Only
nilandfalseare falsy — Unlike Python or JavaScript,0and""are both truthy in Lua. /always returns a float,//is floor division — Since Lua 5.3 the two division operators produce different result types; choose deliberately.^is exponentiation, right-associative, and float-valued —2^3^2evaluates as2^(3^2) = 512, not(2^3)^2 = 64. Lua has no+=,-=,++, or--; writex = x + 1explicitly.#returns length — Works on strings (byte length) and on sequence tables (array-like portion). Behavior on tables with holes is implementation-defined.- Bitwise operators require integers — Available since Lua 5.3; floats with integer values are accepted, but non-integer floats raise an error.
Running Today
All examples can be run using Docker:
docker pull nickblah/lua:5.4-alpine
Comments
Loading comments...
Leave a Comment