Web Analytics

Operators in Lua

Beginner ~20 min read

Operators are symbols that perform operations on values and variables. Lua provides a comprehensive set of operators for arithmetic, comparison, logic, and string manipulation. Understanding operators is fundamental to writing effective Lua code. Let's explore all the operators Lua has to offer!

Arithmetic Operators

Lua supports all standard arithmetic operations:

Operator Description Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Modulo (remainder) 10 % 3 1
^ Exponentiation 2 ^ 3 8
- Unary negation -10 -10

Try It Yourself

Output
Click Run to execute your code
Tip: The ^ operator is for exponentiation, not XOR like in some other languages. Lua doesn't have built-in bitwise operators in Lua 5.1, but they were added in Lua 5.2+.

Relational Operators

Relational operators compare two values and return true or false:

Operator Description Example Result
== Equal to 5 == 5 true
~= Not equal to 5 ~= 3 true
< Less than 3 < 5 true
> Greater than 5 > 3 true
<= Less than or equal 3 <= 5 true
>= Greater than or equal 5 >= 5 true
Important: Lua uses ~= for "not equal", not != like many other languages. Also, use == for comparison, not = (which is assignment).

Logical Operators

Logical operators work with boolean values and use short-circuit evaluation:

Operator Description Example Result
and Logical AND true and false false
or Logical OR true or false true
not Logical NOT not true false

Short-Circuit Evaluation

Lua uses short-circuit evaluation for and and or:

  • and returns the first operand if it's false, otherwise the second
  • or returns the first operand if it's true, otherwise the second
Output
Click Run to execute your code
Best Practice: Use short-circuit evaluation for default values: local value = input or "default". This is a common Lua idiom!

String Concatenation

Lua uses .. (two dots) for string concatenation:

local greeting = "Hello" .. " " .. "World"
print(greeting)  -- Hello World

local name = "Alice"
local message = "Welcome, " .. name .. "!"
print(message)  -- Welcome, Alice!
Tip: Numbers are automatically converted to strings when concatenated: "Score: " .. 100 works fine!

Length Operator

The # operator returns the length of strings and tables:

local str = "Hello"
print(#str)  -- 5

local arr = {10, 20, 30, 40}
print(#arr)  -- 4

Operator Precedence

When multiple operators appear in an expression, Lua follows this precedence (highest to lowest):

  1. ^ (exponentiation)
  2. not, #, - (unary)
  3. *, /, %
  4. +, -
  5. .. (concatenation)
  6. <, >, <=, >=, ~=, ==
  7. and
  8. or
Output
Click Run to execute your code
Best Practice: Use parentheses to make your intentions clear, even if not strictly necessary. (a + b) * c is clearer than a + b * c.

Practice Exercise

Try solving these operator challenges:

Output
Click Run to execute your code

Summary

In this lesson, you learned:

  • Arithmetic operators: +, -, *, /, %, ^
  • Relational operators: ==, ~=, <, >, <=, >=
  • Logical operators: and, or, not
  • String concatenation with ..
  • Length operator #
  • Operator precedence and short-circuit evaluation

What's Next?

Now that you understand operators, you're ready to learn about strings in Lua. In the next lesson, we'll explore string manipulation, pattern matching, and the powerful string library. Let's continue! ๐Ÿš€