Operators

Reference for all expression operators available in Spala.

Operators

Operators are used within Spala expressions to perform calculations, comparisons, and logical operations. They work the same way as JavaScript operators.

Arithmetic operators

| Operator | Description | Example | Result |
|----------|-------------|---------|--------|
| + | Addition (or string concatenation) | 5 + 3 | 8 |
| - | Subtraction | 10 - 4 | 6 |
| * | Multiplication | 6 * 7 | 42 |
| / | Division | 15 / 4 | 3.75 |
| % | Modulo (remainder) | 17 % 5 | 2 |

The + operator also concatenates strings:

'Hello ' + vars.name
// "Hello Alice"

Comparison operators

| Operator | Description | Example | Result |
|----------|-------------|---------|--------|
| == | Equal to | vars.status == 'active' | true/false |
| != | Not equal to | vars.role != 'admin' | true/false |
| > | Greater than | vars.age > 18 | true/false |
| < | Less than | vars.price < 100 | true/false |
| >= | Greater than or equal | vars.count >= 10 | true/false |
| <= | Less than or equal | vars.score <= 50 | true/false |

Spala uses == for equality comparisons (not ===). Type coercion applies, so '5' == 5 is true.

Logical operators

| Operator | Description | Example | Result |
|----------|-------------|---------|--------|
| && | Logical AND | vars.isAdmin && vars.isActive | true if both are true |
| \|\| | Logical OR | vars.role == 'admin' \|\| vars.role == 'owner' | true if either is true |
| ! | Logical NOT | !vars.isDeleted | Inverts the boolean |

Logical operators short-circuit: && stops at the first falsy value, || stops at the first truthy value.

Ternary operator

The ternary operator provides inline conditional expressions:

condition ? valueIfTrue : valueIfFalse

vars.count > 0 ? 'Items found' : 'No items'

Ternary expressions can be nested, but keep them simple for readability:

vars.role == 'admin' ? 'Full access' : vars.role == 'editor' ? 'Edit access' : 'Read only'

Nullish coalescing operator

The ?? operator returns the right-hand value when the left-hand value is null or undefined (but not other falsy values like 0, '', or false).

vars.config.timeout ?? 5000
// Returns vars.config.timeout if it exists, otherwise 5000

Contrast with || which treats 0, '', and false as falsy:

vars.count || 10    // If count is 0, returns 10
vars.count ?? 10    // If count is 0, returns 0

Optional chaining operator

The ?. operator safely accesses nested properties without throwing an error if an intermediate value is null or undefined.

vars.user?.address?.city
// Returns the city if user and address exist, otherwise undefined

Without optional chaining, accessing vars.user.address.city would throw an error if user or address is null.

Works with method calls too:

vars.items?.map('name')

Operator precedence

Operators are evaluated in this order (highest to lowest):

1. ?. -- optional chaining
2. ! -- logical NOT
3. *, /, % -- multiplication, division, modulo
4. +, - -- addition, subtraction
5. >, <, >=, <= -- comparisons
6. ==, != -- equality
7. && -- logical AND
8. || -- logical OR
9. ?? -- nullish coalescing
10. ? : -- ternary

Use parentheses to override precedence when needed:

(vars.a + vars.b) * vars.c

Filters Reference
/reference/filters
Data transformation filters for expressions
Data Types
/reference/data-types
Supported data types in Spala expressions