Expressions

An expression is something that represents a value. It can include numeric constants, quoted strings, variable names, unary and binary operations, and function calls.

+, -, *, /, %, and ^ are called arithmetic operators.

Here is an example of binary arithmetic operators:

An example of the modulo (division remainder) operator is as follows:

An example of the power of operator is as follows:

Relational operators always result in false or true and ask yes or no questions. The relational operators are <, >, <=, >=, ==, ~=.

The == operator tests for equality, and the ~= operator tests for inequality. If the value types are different, then the result is false. Otherwise, Lua compares the values to their types. Numbers and strings are compared in the usual way. Tables and functions are compared by reference as long as two such values are considered equal, only if they are the same object. When a new object is created, the new object is different from the previously existing one.

Here are examples of relational operators. They will display Boolean results and can't be concatenated with strings:

The logical operators in Lua are and, or, and not. All logical operators consider both false and nil as false and anything else as true.

The and operator returns its first argument if the value is false or nil; otherwise, it returns its second argument. The or operator returns its first argument if the value is different from nil and false; otherwise, it returns its second argument. Both and and or use a shortcut evaluation; this means the second operand is evaluated only when necessary. Here are some examples of logical operators:

The not operator always returns true or false:

The string concatenation operator in Lua is denoted by two dots, "..". It takes two values as operands and splices them together. If any of its operands is a number, then it is also converted to a string. Some examples of the concatenation operator are as follows:

The # length operator measures the length of a string or size of a table. The length of a string is simply the number of characters in it. A character is considered one byte. Examples of the length operator are as follows:

The following list shows the operator precedence in Lua displayed from the highest to the lowest priority:

All binary operators are left associative, except for the ^ exponentiation and the.. concatenation, which are right associative. You can use parentheses to change the precedence of an expression.

In cases where two operands of the same precedence compete for operands, the operand belongs to the operator on the left-hand side:

The preceding expression shows both the addition and subtraction operators, which have equal precedence. The second element (the number 4) belongs to the addition operator, so the expression is evaluated mathematically as follows:

Let's focus on the rules of precedence based on priority. Here is an example:

An inexperienced programmer may think that the value of the preceding example is 90 if it were evaluated from left to right. The correct value is 34 because multiplication has a higher precedence than addition, so it is performed first. Adding parentheses to the same expression will make it easier to read: