Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators

Arithmetic

OperatorMeaning
+Addition
-Subtraction, and negation in prefix form (-x)
*Multiplication
/Division
%Remainder, with the sign of the left operand

Both operands must be numbers (error T2001, T2002), and a result that is not finite (such as 1/0) is an error (D1001). If either operand is nothing, the result is nothing.

5 + 2 * 3
=> 11
5 / 2
=> 2.5
-7 % 3
=> -1
input: invoice.json
Account.Order[0].Product[0].(Price * Quantity)
=> 68.9
"5" + 1
=> error T2001
missing + 1
=> undefined

Numbers are 64-bit floats, as in JavaScript, and results are printed the way JavaScript prints them:

0.1 + 0.2
=> 0.30000000000000004

Comparison

OperatorMeaning
=Equal: same type and value; arrays and objects are compared deeply
!=Not equal
<, <=, >, >=Order of numbers, or of strings (by Unicode code point)
inThe left value is one of the values on the right

= and != never fail: values of different types are simply not equal. The ordering operators need two numbers or two strings (errors T2009, T2010). Comparing with nothing gives false.

1 + 1 = 2
=> true
"1" = 1
=> false
{"a": [1, 2]} = {"a": [1, 2]}
=> true
"apple" < "banana"
=> true
"world" in ["hello", "world"]
=> true
"hello" in "hello"
=> true
input: address.json
"01962 001234" in Phone.number
=> true

Boolean

and and or convert their operands to Booleans as $boolean does, and stop as soon as the result is known: the right side of false and x is never evaluated. Negation is a function, $not.

input: library.json
library.books["Aho" in authors and price < 50].title
=> "Compilers: Principles, Techniques, and Tools"

The other book by Aho has no price, and comparing nothing with 50 is false.

0 or "yes"
=> true
$not(true and false)
=> true

Strings

& joins strings. It is the only operator that converts its operands: non-strings become strings as by $string, and nothing becomes the empty string.

input: address.json
FirstName & " " & Surname
=> "Fred Smith"
5 & 0 & true & missing
=> "50true"

Conditionals

condition ? then : else evaluates then when the condition is true, and else otherwise. The condition is converted to a Boolean. Without an else part, a false condition produces nothing.

input: invoice.json
Account.Order.Product.(Price > 100 ? "premium" : "standard")
=> ["standard", "standard", "standard", "premium"]
false ? "yes"
=> undefined

Defaults

OperatorResult
a ?: ba if it is true as a Boolean, else b. A fallback for missing or falsy values (null, false, 0, "")
a ?? ba if it exists (is not nothing), else b. A fallback only for missing values
input: address.json
Other.Misc ?: "n/a"
=> "n/a"
input: address.json
Other.Misc ?? "n/a"
=> null
0 ?? 1
=> 0

Chaining: ~>

value ~> $f(args) calls $f(value, args). Chains read left to right instead of inside out:

input: invoice.json
Account.Order.Product.(Price * Quantity) ~> $sum() ~> $round(1)
=> 336.4
"  Hello  World  " ~> $trim() ~> $uppercase()
=> "HELLO WORLD"

With functions on both sides, ~> composes them into a new function; see Function composition. A regular expression on the right tests a string, which is useful in filters.

Other operators

OperatorMeaningSee
:=Binds a variableVariables
..Integer range, in array constructorsRanges
|location|update, delete|Transform a copy of an objectTransforms
., [...], ^(...), #, @, %, *, **Path stagesPaths

Precedence

From the tightest binding to the loosest:

OperatorsAssociativity
Function call f(...), filter [...], #, @left
.left
Grouping {...} after a pathleft
*, /, %left
+, -, &left
=, !=, <, <=, >, >=, in, ~>, ^(...), ?:, ??left
andleft
orleft
? :right
:=right

Unary minus binds more tightly than any binary operator. Parentheses override precedence:

(5 + 3) * 4
=> 32
1 < 2 and 2 < 3 or false
=> true