Operators are a symbol(s) to perform a specific action. Operands are the values the operator uses in the action.

When using these operators, both operands must be the of the same type.

Comparing by reference means both operands must point to the exact same memory address, the value of its contents don't matter. Example:

local a = {1, 2, 3}local b = {1, 2, 3}local c = a -- c now also holds the same reference to the table that a doesprint(a == b) -- Output: false, both have separate memory addressesprint(a == c) -- Output: true, both variables hold the same reference to the exact same table

<

>

<=

>=

==

~=

<

Operator: Less Than (`a < b`

)

If the value of `a`

is less than `b`

, the result is `true`

, otherwise, `false`

.

Operands Types:

number (compares by value)

string (compares by alphabetical order)

>

Operator: Greater Than (`a > b`

)

If the value of `a`

is greater than `b`

, the result is `true`

, otherwise, `false`

.

Operands Types:

number (compares by value)

string (compares by alphabetical order)

<=

Operator: Less Than or Equal To (`a <= b`

)

If the value of `a`

is less than or equal to `b`

, the result is `true`

, otherwise, `false`

.

Operands Types:

number (compares by value)

string (compares by alphabetical order)

>=

Operator: Greater Than or Equal To (`a >= b`

)

If the value of `a`

is greater than or equal to `b`

, the result is `true`

, otherwise, `false`

.

Operands Types:

number (compares by value)

string (compares by alphabetical order)

==

Operator: Equal (`a == b`

)

If `a`

is exactly equal to `b`

, the result is `true`

, otherwise, `false`

.

Operands Types:

number (compares by value)

string (compares by value)

boolean (compares by value)

function (compares by reference)

table (compares by reference)

userdata (compares by reference)

thread (compares by reference)

~=

Operator: Not Equal (`a ~= b`

)

If `a`

is anything but `b`

, the result is `true`

, otherwise, `false`

.

Operands Types:

number (compares by value)

string (compares by value)

boolean (compares by value)

function (compares by reference)

table (compares by reference)

userdata (compares by reference)

thread (compares by reference)

â€‹

Note: When something is defined as being "truthy", it means that the value is considered to be `true`

by Lua, which is any value that isn't `false`

or `nil`

.

and

or

not

and

Syntax: `a and b`

where a & b are both evaluated as expressions.

If `a`

is truthy, `b`

is the result of the operation, otherwise it is the result of `a`

.

local x = true and 5 -- x is 5local y = x and 10 -- y is 10, x is evaluated to true because 5 isn't a value of false or nillocal z = false and 100 -- false, a is not truthy thus the second value is not the result

or

Syntax: `a or b`

where a & b are both evaluated as expressions.

If `a`

is truthy, result is `a`

, otherwise `b`

.

local x = true or 5 -- x is true, a is truthy so b is never evaluatedlocal y = false or 10 -- y is 10, a is not truthy so b is evaluated and the resultlocal z = (10 > 5) or (x + y) -- true, a is evaluated and 10 > 5 results in true, b is never evaluated

not

Syntax: `not a`

Inverts the truthiness of `a`

, any expression or value that results in true, the value is flipped to false, and vice versa.

local x = not true -- x is falselocal y = not x -- y is true

Here are some more examples pairing these operators together:

local x = 1local y = 2-- ( A operand to 'or' ) ( B operand to 'or' )-- (1 > 2 and <expression> ) or (<fallback expression>)print((x > y) and 'X is greater!' or 'Y is greater!') -- Output: Y is greater!

Since 1 (x) is not a greater number than 2 (y), the result of the `and`

operator is `nil`

because of the `a`

operand resulting in `false`

, the `or`

operator comes in because the result of the `and`

operation is the `a`

operand to `or`

, so the result looks like `nil or 'Y is greater!'`

. In plain English, all that's happening here is: "If my x variable is bigger than our y variable, give me back a string that says X is greater, otherwise, give me a string that says Y is greater."

Addition (+)

Subtraction (-)

Multiplication (*)

Division (/)

Exponentation (^)

Negation (-)

Addition (+)

Syntax: `a + b`

local x = 5 + 10 -- 15local y = 1.2 + 6.3 -- 7.5

Subtraction (-)

Syntax: `a - b`

local x = 5 - 2 -- 3local y = 2.5 - 1.2 -- 1.3

Multiplication (*)

Syntax: `a * b`

local x = 5 * 3 -- 15local y = 1.5 * 1.5 -- 2.25

Division (/)

Syntax: `a / b`

local x = 10 / 2 -- 5local y = 9 / 2.5 -- 3.6

Exponentation (^)

Syntax: `a ^ b`

local x = 5 ^ 2 -- 25local y = 2 ^ 2.5 -- 5.65685424949

Negation (-)

Syntax: `-a`

local x = -5 -- -5local y = 10 -- 10y = -y -- y is now -10y = -x -- y is now 5

Concatenation is an operation that joins two strings together. The result is a copy of both strings joined together, since Lua cannot manipulate the contents of a string itself.

Syntax: `a .. b`

Examples:

local a = "Hello "local b = ", world!"print(a .. b) -- Output: Hello, world!

You can chain these operators however many times you like in any order.

The length operator (#) is primarily used on tables, but can be effective for strings as well. For strings, it returns the number of bytes (usually each byte is a single character). For tables, it returns the length of the table as long as it has no holes and is ordered:

local t = {1, 2, 3}print(#t) -- 3

Depending on the version of Lua you're using, if the array or table has nil values inbetween the other values, the result of the length operator will be different. Any keys that aren't numerical are not counted towards the length.

If you have keys that aren't numbers, or have nil values/holes in your table, a table size function can be implemented by simply using the `pairs`

iterator:

function table.size(t)local size = 0for k, v in pairs(t) dosize = size + 1endreturn sizeendâ€‹local t = {1, 2, 3}local t2 = {a = 5, [2] = 4, 84}print(table.size(t), table.size(t2)) -- Output: 3 3