Opeartors
An operator is a special set of symbols used to perform an operation or conditional evaluation.
Logical
The logical operators for conditional statements are and
, or
, and not
. These operators consider both false
and nil
as “false” and anything else as “true.”
Operator | Description |
---|---|
| Evaluates as |
| Evaluates as |
| Evaluates as the opposite of the condition |
Relational
Relational operators compare two parameters and return a boolean true
or false
.
Operator | Description | Associated metamethod |
---|---|---|
| Equal to |
|
| Not equal to | |
| Greater than | |
| Less than |
|
| Greater than or equal to | |
| Less than or equal to |
|
Arithmetic
Lua supports the usual binary operators along with exponentiation, modulus, and unary negation.
Operator | Description | Example | Associated metamethod |
---|---|---|---|
| Addition | 1 + 1 = 2 |
|
| Subtraction | 1 - 1 = 0 |
|
| Multiplication | 5 * 5 = 25 |
|
| Division | 10 / 5 = 2 |
|
| Exponentiation | 2 ^ 4 = 16 |
|
| Modulus | 13 % 7 = 6 |
|
| Unary negation | -2 = 0 - 2 |
|
Miscellaneous
Miscellaneous operators include concatenation and length.
Operator | Description | Associated metamethod |
---|---|---|
| Concatenates two strings |
|
| Length of table |
|
Compound Assignment
Compound assignment operators are used to set a variable equal to the result of an operation where the first parameter is the variable’s current value.
The expression on the left side of a compound assignment is only evaluated once. For example, if an expression generates a random index in a table, the same index is used for both the operation and the assignment.
Operator | Operation |
---|---|
| Addition |
| Subtraction |
| Multiplication |
| Division |
| Modulus |
| Exponentiation |
| Concatenation |
Last updated