Operators

Assignment

= Assignment Operator

= assigns a value to a variable

local hp = 100;

+= Addition Assignment Operator

+= adds and assigns

local hp = 100;
hp += 10; // hp == 110

-= Subtraction Assignment Operator

-= subtracts and assigns

local hp = 100;
hp -= 40; // hp == 60

*= Multiplication Assignment Operator

*= multiplies and assigns

local hp = 10;
hp *= 2; // hp == 20

/= Division Assignment Operator

/= divides and assigns

local hp = 100;
hp /= 2; // hp == 50

%= Modulo Assignment Operator

%= stores the remainder of the modulo.

local number = 5;
number %= 2; // number == 1

^= Power Assignment Operator

^= raises to a power and assigns

local hp = 10;
hp ^= 2; // hp == 100

??= Nil-coalescing Assignment Operator

??= assigns a value only if the left side is nil

local name = "Lithrun";
name ??= "Valar" // Doesn't do anything, since name is not nil
name = nil;
name ??= "Valar" // name == "Valar"

?!= Inverse Nil-coalescing Assignment Operator

?!= assigns a value only if the left side is not nil

local name = "Lithrun";
name ?!= "Valar" // Valar
name = nil;
name ?!= "Valar" // Doesn't do anything, since the name is nil

Arithmetic Operations

+ Addition Operator

+ adds numbers or joins strings

print(2 + 3) // 5
print("Hello " + "World") // Hello World

- Subtraction Operator

- substracts values

print(10 - 4) // 6

* Multiplication Operator

* multiplies values

print(3 * 4) // 12

/ Division Operator

/ divides values

print(12 / 4) // 3

% Modulo Operator

% returns the remainder

print(10 % 3) // 1

^ Power Operator

^ raises one value to the power of another

print(2 ^ 4) // 16

# Length Operator

# gets the length of a string, table or array

print(#"hello") // 5
print(#[1, 2, 3]) // 3

++ Increment Operator

++ increases a value by 1

local count = 0;
count++; // 1
count++; // 2

-- Decrement Operator

-- decreases a value by 1

local count = 3;
count--; // 2
count--; // 1

Comparison Operators

Comparison operators return either true or false depending if their comparison is met

== Equality Operator

== checks whether two values are equal to each other.

local score = 5;
print(score == 5) // true
print(score == 4) // false

!= Inequality Operator

!= checks whether two values are not equal to each other.

local score = 5;
print(score != 5) // false
print(score != 4) // true

< Less-Than Operator

< checks whether the value is less than the compared value

local score = 5;
print(score < 10) // true
print(score < 5) // false

<= Less-Than-Or-Equal Operator

<= checks whether the value is less than or equal to the compared values

local score = 5;
print(score <= 10) // true
print(score <= 5) // true

> Greater-Than Operator

> checks whether the value is greater than the compared value

local score = 5;
print(score > 10) // false
print(score > 5) // false
print(score > 0) // true

>= Greater-Than-Or-Equal Operator

>= checks whether the value is greater than or equal to the compared values

local score = 5;
print(score >= 10) // false
print(score >= 5) // true

Logical Operators

&& Logical AND Operator

&& returns true if both sides are true

local hp = 50;
local mana = 50;
print(hp > 0 && mana > 100) // false
print(hp > 0 && mana > 0) // true

|| Logical OR Operator

|| returns true if either side is true

local hp = 50;
local mana = 50;
print(hp > 0 || mana > 100) // true
print(hp > 0 || mana > 0) // true

! Logical NOT Operator

! reverses a boolean value

local isDead = true;
print(isDead) // true
print(!isDead) // false

Conditional and Nil Operators

?! Ternary Operator

?! chooses one of two values based on a condition. This is a short way to write an if/else statement

local hp = 50;
local result = hp > 0 ? "Alive" : "Dead"
print(result) // Alive

?? Nil-coalescing Operator

?? uses the right side only if the left side is nil

local nickname = "Lith";
local title = nickname ?? "Sir"; // Lith
nickname = nil;
title = nickname ?? "Sir"; // Sir

?! Inverse Nil-coalescing Operator

?! uses the right side only if the left side is not nil

local nickname = nil;
local title = nickname ?! "Sir"; // nil
nickname = "Lith";
title = nickname ?? "Sir"; // Sir

Access and Call Operators

. Member Access Operator

. accesses a field or method of a table

class Player {
    function GetName() => "Lithrun";
}

local player = new Player();
print(player.GetName()); // With the . you can use properties & functions of the Player

[] Index Operator

[] accesses a value by index or key.

local numbers = 1..5;
local config = {
    volume = 100
}
print(numbers[0]) // 1
print(config["volume"]) // 100

Range Operators

With a range operator, you can create an array of numbers within a certain range.

.. Inclusive Range Operator

.. does from (inclusive) to (inclusive)

for (i in 1..3) {
    print(i)
}
// Output: 1, 2, 3

>.. Left-exclusive Range Operator

>.. goes from (exclusive) to (inclusive)

for (i in 1>..3) {
    print(i)
}
// Output: 2, 3

..< Right-exclusive Range Operator

..< goes from (inclusive) to (exclusive)

for (i in 1..<3) {
    print(i)
}
// Output: 1, 2

>..< Exclusive Range Operator

>..< goes from (exclusive) to (exclusive)

for (i in 1>..<3) {
    print(i)
}
// Output: 2

Categories:

Updated: