---
title: "Operators"
description: "The operators of the Slint language."
---

Slint has arithmetic, comparison, logical, and ternary operators, plus member access and indexing.
Every operator requires operands of specific types; Slint applies no implicit coercions beyond the
few listed here, and mismatched operand types are a compile error.

## Precedence and associativity

The following table lists all operators from loosest to tightest binding.
Operators on the same row share a precedence level.

| Operators              | Description                          | Associativity   |
| ---------------------- | ------------------------------------ | --------------- |
| `? :`                  | Ternary conditional                  | right           |
| `\|\|` `&&`            | Logical *or*, logical *and*          | non-associative |
| `==` `!=` `<` `>` `<=` `>=` | Comparison                     | non-associative |
| `+` `-`                | Addition/concatenation, subtraction  | left            |
| `*` `/`                | Multiplication, division             | left            |
| `+` `-` `!` (unary)    | Plus, negation, logical *not*        | prefix          |
| `.` `[]`               | Member access, indexing              | left            |

Parentheses group a nested expression to override precedence.
There is no modulo operator; use [`Math.mod`](../../global-functions/math/) instead.
A `%` after a number is a unit (percent), not an operator.
Slint has no bitwise, shift, increment, decrement, or assignment operators; a binding uses `:` and a
[two-way binding](../two-way-bindings/) uses `<=>`.

Comparison operators are non-associative: writing `a == b == c` or `a < b < c` is an error, and the
operands must be parenthesized to disambiguate.
`&&` and `||` are likewise non-associative with respect to each other: `a && b || c` is an error, and
one side must be parenthesized.

```slint
export component Example {
    in-out property <int> p: 1 * 2 + 3 * 4; // same as (1 * 2) + (3 * 4)
}
```

## Arithmetic operators

The binary operators `+`, `-`, `*`, and `/` operate on numbers (`int`, `float`, `percent`) and on
types that carry a unit (`length`, `physical-length`, `duration`, `angle`, `rem`).

- `+` and `-` require both operands to be the same type, or one operand to be a plain number.
  Values of two different units cannot be added or subtracted.
- `*` multiplies a value by a number, or two values with units, producing a unit product
  (for example `1px * 1px` has type `px²`).
- `/` divides a value by a number, or by another value.
  Dividing two values of the same unit yields a plain number, so `self.width / 1px` is the width as a `float`.

```slint
export component Example inherits Window {
    in property <length> a;
    in property <length> b;
    out property <length> total: a + b * 2;
}
```

```slint
export component Example {
    in-out property <length> w: 10px + 2px;      // ok, same unit
    in-out property <length> h: 10px * 2;        // ok, length times number
    in-out property <float>  r: self.w / self.h; // ok, length over length
}
```

## String concatenation

The `+` operator also concatenates strings: if either operand is a `string`, the result is a `string`.
A number operand (`int` or `float`) is converted to its string form.
No other type is coerced to `string`, so combining a `string` with, for example, a `length` is an error;
use a [string template](../../property-types/strings/#string) to interpolate other types.

```slint
export component Example {
    out property <string> s: "count: " + 42; // "count: 42"
}
```

## Comparison operators

The operators `==`, `!=`, `<`, `>`, `<=`, and `>=` compare two operands of a common type and
produce a `bool`.

`==` and `!=` test whether the operands are equal.

`<`, `>`, `<=`, and `>=` order numbers, unit-bearing types, and strings; ordering values of any
other type is an error.

```slint
export component Example inherits Window {
    in property <int> a;
    in property <length> w;
    in property <length> h;
    out property <bool> equal: a == 0;
    out property <bool> different: a != 0;
    out property <bool> wider: w > h;
}
```

## Logical operators

The binary operators `&&` and `||` combine two boolean operands into a boolean:
`&&` is true when both operands are true, `||` when either is.

The unary operator `!` negates a boolean.

The operands must be `bool`; no other type is coerced to `bool`.

```slint
export component Example inherits Window {
    in property <bool> a;
    in property <bool> b;
    out property <bool> both: a && b;
    out property <bool> either: a || b;
    out property <bool> neither: !a && !b;
}
```

`&&` and `||` short-circuit: the right operand is evaluated only when the left does not already determine the result.

## Unary operators

The prefix operators are `+`, `-`, and `!`.

`+` and `-` apply to numbers and unit-bearing types, and `!` applies to `bool`.

```slint
export component Example inherits Window {
    in property <length> w;
    in property <bool> flag;
    out property <length> negated: -w;
    out property <bool> toggled: !flag;
}
```

## Ternary operator

`condition ? value1 : value2` evaluates `condition`, a boolean,
and yields `value1` when it is true and `value2` otherwise.

Both branches have the same type, which becomes the type of the whole expression.

A conditional associates to the right,
so `a ? b : c ? d : e` groups as `a ? b : (c ? d : e)`.

```slint
export component Example inherits Window {
    in property <bool> on;
    in property <color> active;
    in property <color> idle;
    out property <color> tint: on ? active : idle;
}
```

## Member access and indexing

A `.` accesses a property, function, or callback of an element by [name](../name-resolution/),
and a field of a [struct](../structs-and-enums/) value:

```slint
export component Example {
    foo := Rectangle {
        x: 42px;
    }
    x: foo.x;
}
```

An `[index]` in square brackets accesses an element of an [array](../../property-types/arrays-and-models/) by
a zero-based integer index.