Operators and Expressions

+ - * / // % ^, unary - Bitwise (5.3+) & | ~ << >>, unary ~

== ~= < > <= >= (note: ~= means not equal)

6.1 Precedence (High to Low)

^                       (right associative)
not  #  -  ~            (unary)
*  /  //  %
+  -
..                      (right associative)
<<  >>
&
~
|
<  >  <=  >=  ~=  ==
and
or

6.2 Logical Operators Return Operands

print(nil or "default")        --> default
print(false and "x")           --> false
print(1 and 2)                 --> 2
print(nil and nil.field)       --> nil (short-circuit: the right side isn't evaluated)
local max = (a > b) and a or b -- ternary idiom (careful: fails if `a` is false/nil)
local x = cond and value or default

6.3 Comparisons

== compares by value for numbers, strings, booleans, and nil, and by reference for tables, functions, userdata, and threads (unless __eq is defined).

Comparing different types with < raises an error (1 < "2" is an error). 1 == "1" is false.

Strings compare lexicographically (locale-dependent).