Operators#

Lua’s operator set is small. Arithmetic on numbers, comparison on values, and / or / not on truthy values, .. on strings, # for length, and (5.3+) bitwise on integers.

For the values these act on, see Types. For how operators participate in conditions and loops, see Control flow.

Arithmetic#

Operator

Meaning

+

addition

-

subtraction (and unary negation)

*

multiplication

/

float division (always returns float)

//

floor division (5.3+); integer / integer stays integer

%

modulo; result has the sign of the divisor

^

exponentiation; always returns float

print(7 / 2)     --> 3.5    float division
print(7 // 2)    --> 3      floor division
print(-7 % 3)    --> 2      sign follows divisor
print(2^10)      --> 1024.0

Relational#

Operator

Meaning

==

equal; for tables, equality is reference-identity unless overridden by __eq

~=

not equal

<, <=, >, >=

ordering; defined on numbers and on strings (lexicographic)

print(1 == 1.0)             --> true
print("abc" < "abd")        --> true
print({} == {})             --> false   different tables

Comparison between incompatible types (number vs string, table vs string) raises an error; the operator coerces explicitly.

Logical#

and and or short-circuit and return one of their operands, not a clean boolean. not returns a clean true / false.

print(1 and 2)      --> 2
print(nil and 2)    --> nil
print(false or 2)   --> 2
print(1 or 2)       --> 1
print(not 1)        --> false

The x and a or b idiom is Lua’s ternary; it works as long as a is itself truthy.

local label = (n > 0) and "positive" or "non-positive"

Concat#

.. concatenates two strings (or numbers; numbers are coerced to their string form). Repeated concatenation in a loop is quadratic; use table.concat for large joins.

print("hello, " .. "world")
print("count=" .. 42)

local parts = {}
for i = 1, 100 do parts[#parts + 1] = tostring(i) end
print(table.concat(parts, ","))

Length#

Unary # returns the length of a string (in bytes) or a sequence (a table with integer keys 1..n and no nil holes).

print(#"operator")          --> 8
print(#{10, 20, 30})        --> 3

For tables with nil holes, # returns some boundary and is not defined. Use ipairs to walk a sequence and pairs to count an arbitrary table.

Bitwise#

Lua 5.3+ has bitwise operators on integers. Earlier versions used the bit32 library (Lua 5.2) or BitOp (LuaJIT).

Operator

Meaning

&

bitwise AND

|

bitwise OR

~

bitwise XOR (binary) / NOT (unary)

<<

left shift

>>

right shift (logical, not arithmetic)

print(0xff & 0x0f)     --> 15
print(1 << 8)          --> 256
print(~0 & 0xff)       --> 255

Precedence#

Highest to lowest. Operators on the same row associate left-to-right; ^ and .. associate right-to-left.

Operator

Notes

^

right-associative

unary not, #, -, ~

*, /, //, %

+, -

..

right-associative

<<, >>

&

~ (binary XOR)

|

<, >, <=, >=, ~=, ==

and

or

When in doubt the operator parenthesises. The compiler does not warn about precedence pitfalls.

References#