FANDOM


Logical OperatorsEdit

EssentialsEdit

Logical operators are Operators logically resolve the expression. When I refer to "true" in the following table, I mean anything that isn't nil or false. In the not category when I say "opposite of boolean" I mean opposite of nil or false (which is true), and anything else is returned false.

Logical operator list.
and or not
If a and b are both true. Returns last operand. If a or b is true. Returns true one. Returns the opposite of the boolean.

SamplesEdit

Sample
The following is an example of the and operator.

Source:

print(1 and 2)
print(2 and 3)
print(nil and true)
print(false and true)
print(nil and nil)
Output:

2 3 nil false nil



Sample
The following is an example of the or operator.

Source:

print(1 or 2)
print(2 or 3)
print(nil or true)
print(false or true)
print(nil or nil)
Output:

1 2 true true nil



Sample
The following is an example of the not operator.

Source:

print(not 1)
print(not 2)
print(not nil)
print(not false)
print(not true)
Output:

false false true true false



ExtraEdit

These operators are commonly used within a conditional. By taking them out of the conditional, you can create much better code. For example, you can use the or operator to create default values. This is great for creating optional arguments in a function constructor.

Sample
The following is an example of having a default value using or.

Source:

local function foo(a)
       print(a or 'No argument specified')
end
foo('Hello world')
foo()
Output:

Hello world
No argument specified

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.