# Operators

Published Sep 23, 2023Updated May 15, 2024
Contribute to Docs

Operators perform broadly logical or mathematical computations. An operator takes in one or more arguments, performs some computations based on those arguments, and returns a value.

## Logical Operators

Logical operators mirror the behavior of logical concepts like conjunction, disjunction, and negation. Because Luau is not a strongly-typed language, logical operators in Luau will accept arguments other than `true` and `false`. Note also that Luau treats a value of `nil` as equivalent to `false` in the context of a logical operator.

Logical Operator Behavior
`and` Returns the first argument if the first argument is `false`; otherwise returns the second argument
`or` Returns the first argument if the first argument is not `false`; otherwise returns the second argument
`not` Returns `true` if and only if the argument is `false`; otherwise returns `false`

### Examples

```print(true and false) -- falseprint(false and false) -- falseprint(true and true) -- trueprint(1 and 2) -- 2print("Hello" and "World") -- Worldprint(true or false) -- trueprint(false or false) -- falseprint(true or true) -- trueprint(1 or 2) -- 1print("Hello" or "World) -- Helloprint(not false) -- trueprint(not nil) -- trueprint(not true) -- falseprint(not 1) -- falseprint(not "Hello World") -- false
```

## Mathematical Operators

Mathematical operators include arithmetic and relational (comparative) operators.

### Relational Operators

Relational operators compare two arguments numerically (greater than, equal to, etc.) and return a boolean value of `true` or `false`.

Relational Operator Behavior
`==` Returns `true` if the two arguments are equal; otherwise returns `false`
`~=` Returns `true` if the two arguments are not equal; otherwise returns `false`
`>` Returns `true` if the first argument is greater than the second argument; otherwise returns `false`
`<` Returns `true` if the first argument is less than the second argument; otherwise returns `false`
`>=` Returns `true` if the first argument is greater than or equal to the second argument; otherwise returns `false`
`<=` Returns `true` if the first argument is less than or equal to the second argument; otherwise returns `false`

#### Examples

```print(5 == 5) -- trueprint("arts" = "sciences") -- falseprint(0 ~= 9) -- trueprint(true ~= true) -- falseprint(15 > 2) -- trueprint(6 > 9) -- falseprint("apple" >= "apple") -- trueprint(7 >= 8)  -- falseprint(false <= false) -- trueprint(3 <= 0) -- false
```

## Arithmetic Operators

Arithmetic operators perform the basic operations of arithmetic (addition, subtraction, etc.) on the supplied arguments.

Arithmetic Operator Behavior
`+` Returns the sum of two arguments
`-` Returns the value of the second argument subtracted from the first argument
`*` Returns the product of two arguments
`/` Returns the value of the first argument divided by the second argument
`^` Returns the value of the first argument to the power of the second argument
`%` Returns the remainder of the first argument divided by the second argument

### Examples

```print(3 + 4) -- 7print(2.3 - 7.8) -- -5.5print(0 * 4.2) -- 0.0print(8 / 3) -- 2.6666666666667print(4 ^ 6) -- 4096.0print(8 % 2) -- 0
```

## Other Operators

There are a couple of operators that do not fit into the categories above.

Operator Behavior
`..` Returns a concatenation of two string arguments
`#` Returns the number of elements in a table

### Examples

```print("Finally " .. "together") -- "Finally together"print(#{"#", "goes", "crazy"}) -- 3
```

## Compound Assignment

Several operators can be combined with `=` to form a compound operator that updates the value of a variable by performing an operation on it.

Compound Operator Behavior
`+=` Adds argument to initial variable value
`-=` Subtracts argument from initial value
`*=` Multiplies argument by initial value
`/=` Divides initial value by argument
`%=` Variable updated to remainder of initial value divided by argument
`^=` Variable updated to initial value to the power of the argument
`..=` Concatenates initial value with argument

### Examples

```initial_value = 8print(initial_value += 10) -- 18print(initial_value -= 4) -- 4print(initial_value *= 3) -- 24print(initial_value /= 2) -- 4print(initial_value % = 3) -- 2print(initial_value ^= 0) -- 1.0
initial_value = "8"print(initial_value ..= " ball") -- 8 ball
```