Operators modify the values from variables and function calls to form expressions. The simplest expressions don’t need any operators. There are different operators for table values, strings, numbers, and booleans. There are also comparison operators, the list operators comma and semi-colon, and grouping parenthesis.
Operators are evaluated in a predictable order. From highest to lowest precedence:
Grouping part of an expression in parenthesis will cause those inside part of the expression to be evaluated before those on either side.
Operators with values on the left and right side will evaluate the left side first. Except for the exponent (
^) and concatenation (
..) operators; those two evaluate the right side before the left.
| ||Creates a new table using the values in the list.|
| table ||Refers to the value of a table index.|
| table ||Refers to the value of a table field.|
| table ||Refers to a method in the table.|
| ||This is called the “length” operator, but isn’t really.|
| left ||Concatenation operator. Joins two strings.|
Concatenation of numbers is allowed. The value is converted to a decimal representation of the number then joined as a string.
Object types can support concatenation through metamethods.
| number ||Exponent operator.|
| ||Negates the number.|
| num ||Modulo operator. Returns the integer remainder when num is divided by den.|
| num ||Division operator. The result may be a real number even if both numbers are integers.|
| left ||Multiplication operator.|
| left ||Subtraction operator.|
| left ||Addition operator.|
Object types can support arithmetic through metamethods.
| left ||Equal to.|
| left ||Not equal to.|
| left ||Less than or equal to.|
| left ||Greater than or equal to.|
| left ||Less than.|
| left ||Greater than.|
Integer and real numbers can be compared with each other. For all other values, the types must be the same in order to make a comparison. Two different types can be compared using
~=, and they will always be not equal. Attempting to compare different types as less than or greater than is an error. Object types are equal if they reference the same object. Comparison of object types can be customized with metamethods.
A common mistake when starting out with Lua is to write “not equal” as either
<>, because those operators are common in other languages.
| first ||Conjunction.|
| first ||Alternation.|
not operator has a very high precedence, so using parenthesis will often be required with it. If the value evaluates to
false, then the result of the operator is
true. Otherwise it is
or operators will evaluate the values as booleans, but will not convert them. So the result is either the first or second value. In some cases, only the first value is sufficient to complete the operation. For the
and operator, if the first value is false, then the logical result of the operation will always be false. If the first value to the
or operator is true, then the result is always true. When these cases occur, the operators will not evaluate of the second value. This means that any side-effects of getting the value, such as calling a function, will also not occur. It also means that while the second value is being evaluated, then the condition tested by the first value will be false or true depending on the operator used.
The logical operators are useful for creating simple conditional expressions without having to create an if-then-else block. This is most often helpful when testing for a nil value.
function get(directory, name) -- If the name is not given, use a default name name = name or "default" local table = directory[name] -- Only try retrieving the value if the table exists return table and table.value end -- The long way function get(directory, name) if not name then name = "default" end local table = directory[name] if table then return table.value else return nil end end