Operators

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

Relational Operators

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 does
print(a == b) -- Output: false, both have separate memory addresses
print(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)

Logical Operators

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.

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 5
local y = x and 10 -- y is 10, x is evaluated to true because 5 isn't a value of false or nil
local z = false and 100 -- false, a is not truthy thus the second value is not the result

Here are some more examples pairing these operators together:

local x = 1
local 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."

Mathematical Operators

Syntax: a + b

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

Concatenation

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.

Length

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 = 0
    for k, v in pairs(t) do
        size = size + 1
    end
    return size
end

local t = {1, 2, 3}
local t2 = {a = 5, [2] = 4, 84}
print(table.size(t), table.size(t2)) -- Output: 3 3

Last updated