PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Jul 10 2018, 11:35 AM PST 2 min

An operator is a special set of symbols that go between and around variable names to perform an operation.

Logical

The word “truthy” in the following table refers to anything that isn’t nil or false.

Operator Simple description Technical Description
a and b Return true only if a and b are true Return b if a is truthy, otherwise return a
a or b Return true if either a or b is true Return a if a is truthy, otherwise return b
not a Return the opposite of a Return false if a is truthy, else return true

Relational

Relational operators are operators that compare two parameters and return a boolean. There are several operators.

Operator Name Associated metamethod
== Equal to __eq
~= Not equal to
> > Greater than >
< Less than __lt
>= Greater than or equal to
<= Less than or equal to __lte

Arithmetic

Arithmetic operators are operators like those you would find on a calculator.

Operator Bame Example Associated metamethod
^ Exponentiation 2 ^ 4 = 16 __pow
- Unary negation -2 = 0 - 2 __unm
* Multiplication 5 * 5 = 25 __mul
/ Division 10 / 5 = 2 __div
% Modulus 13 % 7 = 6 __mod
+ Addition 1 + 1 = 2 __add
- Subtraction 1 - 1 = 0 __sub

Miscellaneous

Miscellaneous operators are operators that perform a task that cannot be categorized as the other three types.

Operator Name Associated metamethod
x .. y Concatenation __concat
#x Length of table __len
x(...) Invocation __call
x[var] rowspan=2 __index
x.name rowspan=2 __index
x[var] Indexed assignment newindex
x.name |- Indexed assignment newindex
Tags:
  • lua
  • coding
  • concept