Skip to content

Commit 8ec3c98

Browse files
committed
docs: create a table describing operator precedence and associativity
1 parent a337aa1 commit 8ec3c98

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,20 @@ When using multiple operators, they will be evaluated according to their precede
208208
filter(.age >= 18 and .age <= 65)
209209
```
210210

211-
Note that some operators, like `and`, `or`, `+`, and `-`, support more than two values and are evaluated left-to-right (left associative), like `2 + 3 + 4` Others, like `^` and `==`, do not support more than two values. If needed, it is always possible to use parenthesis, like `(2 ^ 3) ^ 4`.
211+
Note that some operators, like `and`, `or`, `+`, and `-`, support more than two values and are evaluated left-to-right, like `2 + 3 + 4`. Others, like `^` and `==`, do not support more than two values. If needed, it is always possible to use parenthesis, like `(2 ^ 3) ^ 4`.
212212

213213
The operators have the following precedence, from highest to lowest:
214214

215-
- `|`
216-
- `^`
217-
- `*`, `/`, `%`
218-
- `+`, `-`
219-
- `>`, `>=`, `<`, `<=`, `in`, `not in`
220-
- `==`, `!=`
221-
- `and`
222-
- `or`
215+
| Precedence | Associativity | Operators |
216+
|-----------------------------|---------------|--------------------------------------|
217+
| 8: pipe | left-to-right | `\|` |
218+
| 7: exponentiation | n/a | `^` |
219+
| 6: multiplicative operators | left-to-right | `*`, `/`, `%` |
220+
| 5: additive operators | left-to-right | `+`, `-` |
221+
| 4: relational operators | n/a | `>`, `>=`, `<`, `<=`, `in`, `not in` |
222+
| 3: equality operators | n/a | `==`, `!=` |
223+
| 2: and | left-to-right | `and` |
224+
| 1: or | left-to-right | `or` |
223225

224226
See section [Function reference](reference/functions.md) for a detailed overview of all available functions and operators.
225227

reference/functions.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ jsonquery(data, 'filter(regex(.message, "like|awesome", "i"))')
10851085

10861086
## add (`+`)
10871087

1088-
Add two or more values. The operator is left associative.
1088+
Add two or more values. The operator evaluated left-to-right (left associative).
10891089

10901090
```text
10911091
a + b
@@ -1111,7 +1111,7 @@ jsonquery(user, '(.firstName + " ") + .lastName')
11111111

11121112
## subtract (`-`)
11131113

1114-
Subtract two or more values. The operator is left associative.
1114+
Subtract two or more values. The operator evaluated left-to-right (left associative).
11151115

11161116
```text
11171117
a - b
@@ -1130,7 +1130,7 @@ jsonquery(data, '.a - .b') // 4
11301130

11311131
## multiply (`*`)
11321132

1133-
Multiply two or more values. The operator is left associative.
1133+
Multiply two or more values. The operator evaluated left-to-right (left associative).
11341134

11351135
```text
11361136
a * b
@@ -1149,7 +1149,7 @@ jsonquery(data, '.a * .b') // 12
11491149

11501150
## divide (`/`)
11511151

1152-
Divide two or more values. The operator is left associative.
1152+
Divide two or more values. The operator evaluated left-to-right (left associative).
11531153

11541154
```text
11551155
a / b
@@ -1185,11 +1185,13 @@ jsonquery(data, '.a ^ .b') // 8
11851185

11861186
## mod (`%`)
11871187

1188-
Calculate the remainder (the modulus) of `a` divided by `b`, like `a % b`. The modulus operator does not support more than two values, so if you need to calculate a chain of multiple modulus operators you'll have to use parenthesis, like `(a % b) % c`.
1188+
Calculate the remainder (the modulus) of `a` divided by `b`, like `a % b`. Multiple values can be chained, in that case they are evaluated left-to-right (left associative).
11891189

11901190
```text
11911191
a % b
1192+
a % b % c % ...
11921193
mod(a, b)
1194+
mod(a, b, c, ...)
11931195
```
11941196

11951197
Examples:

0 commit comments

Comments
 (0)