Operators
DLiteScript provides a variety of operators for performing operations on values.
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numeric values.
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 10 / 2 | 5 |
% | Modulo | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
Examples
| |
String Concatenation
The + operator also works for string concatenation:
| |
Array Concatenation
The + operator concatenates arrays as well:
| |
Assignment Operators
Assignment operators assign values to variables.
Basic Assignment
| Operator | Description | Example |
|---|---|---|
= | Assignment | x = 5 |
Compound Assignment
Compound assignment operators perform an operation and assign the result in one step.
| Operator | Description | Example | Equivalent to |
|---|---|---|---|
+= | Add and assign | x += 5 | x = x + 5 |
-= | Subtract and assign | x -= 5 | x = x - 5 |
*= | Multiply and assign | x *= 5 | x = x * 5 |
/= | Divide and assign | x /= 5 | x = x / 5 |
%= | Modulo and assign | x %= 5 | x = x % 5 |
**= | Exponentiate and assign | x **= 2 | x = x ** 2 |
Examples
| |
Comparison Operators
Comparison operators compare two values and return a boolean result.
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 5 > 3 | true |
>= | Greater than or equal to | 5 >= 5 | true |
< | Less than | 3 < 5 | true |
<= | Less than or equal to | 3 <= 5 | true |
Type Comparison
Comparisons between different types are allowed:
| |
Logical Operators
Logical operators perform boolean logic operations.
| Operator | Description | Example | Result |
|---|---|---|---|
&& | Logical AND | true && true | true |
|| | Logical OR | true || false | true |
! | Logical NOT | !false | true |
Examples
| |
Short-Circuit Evaluation
Logical operators use short-circuit evaluation:
&&returnsfalsewithout evaluating the right operand if the left isfalse||returnstruewithout evaluating the right operand if the left istrue
Special Operators
Spread Operator (...)
The spread operator expands array elements, typically used in function calls to pass array elements as individual arguments.
| |
Index Operator ([])
The index operator accesses elements in an array by their position (zero-based).
| |
You can also use it to assign values:
| |
Operator Precedence
Operators are evaluated in the following order (highest to lowest precedence):
**(Exponentiation)!(Logical NOT)*,/,%(Multiplication, Division, Modulo)+,-(Addition, Subtraction)<,<=,>,>=(Comparison)==,!=(Equality)&&(Logical AND)||(Logical OR)
Use parentheses to explicitly control evaluation order:
| |