In programming, operators are symbols or characters that perform specific actions on values or variables. They allow you to manipulate and perform calculations on data in your program.

There are 4 types of operators:
(1) Arithmetic (addition, subtraction, etc.)
(2) Comparison (greater than, less than, etc.)
(3) Logical (manipulate conditions)
(4) Bitwise (manipulate bits in binary data)

There is one extra operator for concatenation, combining a string with other values into a longer string: (..)

a = "hello"
b = "world"
print( a .. " " .. b ) --hello world

Arithmetic Operators

These are used to manipulate numbers and perform basic mathematical operations.

+ addition
- subtraction
* multiplication
/ division
% modulo (get remainder of division)
^ exponentiation (to the power of)
3+2 --5

8-2 --6

7*3 --21

40/5 --6

10%7 --3

4^3 --64

Comparative Operators

These are used to manipulate values (usually numbers) and perform basic comparisons between two values, usually in an if statement.

== equal to
~= not equal to
!= not equal to (alternative)
< less than
> greater than
<= less than or equal to
>= greater than or equal to
if a==b then print("equal") end

if a~=b then print("not equal") end

if a!=b then print("not equal") end

if a<b then print("greater than") end

if a<=b then print("less than or equal") end

if a>=b then print("greater than or equal") end

The equal and not equal comparison works with numbers, strings, booleans, and functions.

Logical Operators

These are used to manipulate or compare values (usually booleans). All logical operators consider both false and nil as false and anything else as true.

and Returns the first false value it encounters or the last true value if all values are true. It can be used to check multiple conditions at once. 
print( true and true )   --true
print( true and false )  --false
print( false and true )  --false
print( false and nil )   --false
print( 10 and 3 )        --3
print( nil and 3 )       --nil
print( "a" and "b" )     --b
print( "a" and nil )     --nil


or Returns the first true value it encounters or the last false value if all values are false. It can be used to choose between multiple options.
print( false or false ) --false
print( true or false )  --true
print( false or true )  --true
print( false or nil )   --nil
print( 10 or 3 )        --10
print( nil or 3 )       --3
print( "a" or "b" )     --a
print( nil or "b" )     --b


not Reverses the Boolean value of an expression. It returns "true" if the expression is false and "false" if the expression is true.
print( not true )    --false
print( not false )   --true
print( not -3 )      --false
print( not 0 )       --false
print( not "b" )     --false
print( not nil )     --true

A common way to use these logical operators is to combine multiple condition checks, for example:

if a and b then
	print("both true")

if a or b then
	print("one or both true")

if not a and b or c then
	print("it's complicated")

Bitwise Operators (advanced)

& bitwise and
| bitwise or
^^ bitwise xor
~ bitwise not
<< bitwise shift left
>> bitwise shift right
>>> bitwise logical right shift
<<> bitwise rotate left
>>< bitwise rotate right

See Bitwise Operations for more.

Precedence of Operators

Operator precedence determines the order in which operators are evaluated in an expression. It ensures that expressions are evaluated in a consistent and predictable manner. When multiple operators are used in a single expression, operator precedence helps determine which operators are evaluated first.

highest precedence

( )
* / %
+ -
<< >>
< > <= >= ~= ==

lowest precedence


14 May 2023