Skip to content

What Are Operators?

What Are Operators?

Operators⚓︎

Operators are a set of symbols that you can use to do cool and quirky things.

Logical operators⚓︎

The logical operators are and, or, not. These operators considers false and nil as "false" and everything else as "true".

And⚓︎

and will evaluate as true if both conditions are true

local trueStatement = (1 + 1 == 2) and (3 * 2 == 6)
local falseStatement = (2 - 1 == 1) and (6 / 2 == 5)

print(trueStatement)
print(falseStatement)
true
false

You can also use and to check a condition when assigning a value to a variable without using an if statement.

local rightAnswer = (2 + 2 == 4) and "right"
-- Since 2 + 2 is indeed 4, it will assign "right" to rightAnswer

print(rightAnswer)
right

Or⚓︎

or will evaluate as true if one of the conditions are true

local trueStatement = (2 + 2 == 69) or (2 * 2 == 4)
-- One of these conditions are true, hence it will evaluate as true

local falseStatement = (6 ^ 6 == 89) or (2 - 9 == -420)
-- Both conditions are false, hence it will evaluate as false

print(trueStatement)
print(falseStatement)
true
false

Just like and, you can also use or when assigning values to variables.

local answer = (2 + 2 == 5) and "right" or "wtf wrong !!!!"
--[[ Since 2 + 2 is not 5, it will assign "wtf wrong !!!!" to answer, 
instead of assigning "right" to it.
]]

print(answer)
wtf wrong !!!!

Not⚓︎

not will evaluate as the opposite of the condition. For example, not true --> false, not false --> true

local trueStatement = (2 + 2 == 4) -- true
local falseStatement = (2 + 2 == 5) -- false

print(not trueStatement)
print(not falseStatement)
false
true

Relational⚓︎

Relational operators are used to compare 2 values and will return a boolean (true or false)

Equal to (==)⚓︎

As the name suggests, this operator checks if one value is equal to another value.

print(2 == 1 + 1)
print(3 == 2)
true
false

Not equal to (~=)⚓︎

This operator checks if one value does not equal to another value

print("joe" ~= "willie")
print(1 ~= 0 + 1)
true
false

Greater than (>)⚓︎

This operator checks if one value is greater than to another value

print(3 > 2)
print(-1 > 4)
true
false

Less than (<)⚓︎

This operator checks if one value is less than to another value

print(2 < 3)
print(4 < -1)
true
false

Greater than or equal to (>=)⚓︎

This operator checks if one value is great than or equal to to another value

print(3 >= 1)
print(3 >= 3)
print(3 >= 5)
true
true
false

Less than or equal to (<=)⚓︎

This operator checks if one value is less than or equal to to another value

print(1 <= 3)
print(1 <= 1)
print(5 <= 3)
true
true
false

Arithmetic⚓︎

Lua supports the usual binary operators along with exponentiation (^), modulus (%), and unary negation (-). These are pretty self explanatory, so I won't be providing any explanations.

Addition (+)⚓︎

print(2 + 2)
print(3 + 6)
4
9

Subtraction (-)⚓︎

print(4 - 2)
print(5 - 2)
2
3

Multiplication (*)⚓︎

print(2 * 3)
print(3 * 3)
6
9

Division (/)⚓︎

print(6 / 2)
print(1 / 2)
3
0.5

Exponentiation (^)⚓︎

print(2 ^ 3)
print(3 ^ 3)
8
27

Modulus (%)⚓︎

print(2 % 2)
print(13 % 6)
0
1

Unary negation (-)⚓︎

print(-4)
-4

Miscellaneous⚓︎

Miscellaneous operators include concatenation and length.

Concatenation (..)⚓︎

Concatenates 2 values together. You can only concatenate strings and numbers

print("hello ".."world!!!!")
hello world!!!!

Length (#)⚓︎

If used on a table, specifically an array, it will return the number of elements in that array. If used on a string, it will return the amount of characters in that string, spaces are counted too

local array = {1,5,6,8,9}
local leString = "hellooo"
print(#array)
print(#leString)
5
7

Thanks for reading !!!⚓︎

If there are any mistakes found in this article, please report the artcile!

Comments