Operators

An operator is a symbol for performing an operation or conditional evaluation.

Logical

Logical operators return values depending on the boolean values of the given arguments. If an argument isn't false or nil, then the operator evaluates it as true. Unlike many other languages, Luau considers both zero and the empty string as true. The following table summarizes how logical operators behave in conditionals.

Operator Descriptions
and Evaluates as true only if both conditions are true
or Evaluates as true if either condition is true
not Evaluates as the opposite of the condition

and

The binary operator and returns one of the two arguments. If the first argument evaluates to true, then it returns the second argument. Otherwise, it returns the first argument.


1print(4 and 5) -- 5
2print(nil and 12) -- nil
3print(false and 12) -- false
4print(false and true) -- false
5print(false and false) -- false
6print(true and false) -- false
7print(true and true) -- true
8

You can use and to test multiple conditions in control structures such as if statements and while loops. For example, the following ifthen statement tests that two conditions are both true:


1local pasta = true
2local tomatoSauce = true
3
4if pasta == true and tomatoSauce == true then
5 print("We have spaghetti dinner")
6else
7 print("Something is missing...")
8end
9-- Output: We have spaghetti dinner
10

or

The binary operator or returns one of the two arguments. If the first argument evaluates to true, then it returns the first argument. Otherwise, it returns the second argument.


1local y = x or 1
2print(y) -- 1 because x doesn't exist and is therefore nil
3
4local d = false
5local e = d or 1
6print(e) -- 1 because d is false
7
8print(4 or 5) -- 4
9print(nil or 12) -- 12
10print(false or 12) -- 12
11print(false or true) -- true
12print(false or false) -- false
13print(true or false) -- true
14print(true or true) -- true
15

You can use or to perform complex logical tests in control structures. For example, the following ifthen statement tests whether two conditions are true or a third condition is true:


1local pasta = false
2local tomatoSauce = true
3local garlicBread = true
4
5if (pasta == true and tomatoSauce == true) or garlicBread == true then
6 print("We have either spaghetti dinner OR garlic bread")
7else
8 print("Something is missing...")
9end
10
11-- Output: We have either spaghetti dinner OR garlic bread
12

not

The unary operator not returns the opposite boolean value of the argument. If the argument is false or nil, then it returns true. Otherwise, it returns false.


1print(not true) -- false
2print(not false) -- true
3print(not nil) -- true
4print(not "text") -- false
5print(not 0) -- false
6

You can use the not operator to trigger a conditional or loop if a variable is false or nil.


1local nilVariable -- Variable is declared but has no value, so it's nil
2local falseVariable = false -- Variable is declared with value of false
3
4if not nilVariable then
5 print(nilVariable) -- Outputs "nil" because nil isn't true
6end
7
8if not falseVariable then
9 print(falseVariable) -- Outputs "false" because false isn't true
10end
11

You can also use the not operator to test for the opposite of a entire multi-condition statement. In the following code sample, the ifthen conditional tests that it's neither true that three is greater than four nor is it true that five is greater than four.


1local three = 3
2local four = 4
3local five = 5
4
5if not (three > four or five < four) then
6 print("Three is less than 4 and five is greater than 4.")
7end
8
9-- Output: Three is less than 4 and five is greater than 4.
10

Relational

Relational operators compare two parameters and return a boolean: true or false.

Operator Description Example Associated metamethod
== Equal to 3 == 5false __eq
~= Not equal to 3 ~= 5true
> Greater than 3 > 5false
< Less than 3 < 5true __lt
>= Greater than or equal to 3 >= 5false
<= Less than or equal to 3 <= 5true __le

Arithmetic

Lua supports the usual binary operators along with exponentiation, modulus, and unary negation.

Operator Description Example Associated metamethod
+ Addition 1 + 1 = 2 __add
- Subtraction 1 - 1 = 0 __sub
* Multiplication 5 * 5 = 25 __mul
/ Division 10 / 5 = 2 __div
^ Exponentiation 2 ^ 4 = 16 __pow
% Modulus 13 % 7 = 6 __mod
- Unary negation -2 = 0 - 2 __unm

Compound Assignment

You can use compound assignment operators to set a variable equal to the result of an operation where the first parameter is the variable's current value.

The operation in a compound assignment occurs once. For example, if an expression generates a random index in a table, Luau uses the same index for both the operation and the assignment.

In the following examples, suppose local x = 3.

Operator Operation Example New Value of x
+= Addition x += 2 5
-= Subtraction x -= 2 1
*= Multiplication x *= 2 6
/= Division x /= 2 1.5
%= Modulus x %= 2 1
^= Exponentiation x ^= 2 9
..= Concatenation x ..= " World!" "2 World!"

Miscellaneous

Miscellaneous operators include concatenation and length.

Operator Description Example Associated metamethod
.. Concatenates two strings print("Hello " .. "World!") __concat
# Length of table If tableVar = {1, 2, 3}, then #tableVar == 3. __len