In Lua, we have and
, or
, and not
as logical operators. These operators can be used to evaluate the expression.
In Lua, everything that is not
nil
orfalse
is handled astrue
in logical expressions.
and
operatorThe and
operator is used as a binary and returns the first operand if it’s false
. Otherwise, it will return the second.
print('a string' and false)print('a string' and true)print(true and 12)
false
true
12
or
operatorThe or
operator returns the first operand if it’s true. Otherwise, it returns the second.
print("a" or false)print(true or 1)
a
true
not
operatorThe not
operator is a unary operator and will return true
if the operand is nil
or false
. Otherwise, it will return true
.
print(not false)print(not true)print(not 11)
true
false
false