Skip to content

XEG Operators

Higher precedence binds tighter. Parentheses group without adding anything to the expression. This page is generated from spec/expression-reference.json; the builtins are on XEG Builtins and the full grammar on Expressions.

PrecedenceOperatorNameFormAssociativitySummary
10.memberpostfixlefta.key reads an own key of an object.
10[ ]indexpostfixlefta[i] reads an array element, a string’s UTF-16 code unit, or an object key.
10( )callpostfixleftname(args) calls a builtin.
9!notprefixrighttrue when the operand is falsy, false otherwise.
9-negateprefixrightThe numeric negation of the operand.
9+plusprefixrightThe operand unchanged when it is a number.
8*multiplybinaryleftNumeric multiplication.
8/dividebinaryleftNumeric division.
8%remainderbinaryleftThe remainder of numeric division, taking the sign of the dividend.
7+add or concatenatebinaryleftNumeric addition, or string concatenation when either side is a string.
7-subtractbinaryleftNumeric subtraction.
6<less thanbinaryleftWhether a orders before b.
6<=less than or equalbinaryleftWhether a orders before or equal to b.
6>greater thanbinaryleftWhether a orders after b.
6>=greater than or equalbinaryleftWhether a orders after or equal to b.
5===equalbinaryleftStrict equality between two primitives.
5==equal (loose spelling)binaryleftAnother spelling of ===.
5!==not equalbinaryleftThe negation of ===.
5!=not equal (loose spelling)binaryleftAnother spelling of !==.
4&&logical andbinarylefta && b is a when a is falsy, otherwise b.
3||logical orbinarylefta || b is a when a is truthy, otherwise b.
2??nullish coalescingbinarylefta ?? b is a unless a is null, in which case it is b.
1? :conditionalternaryrighttest ? a : b evaluates to a when test is truthy and b otherwise.

a.key reads an own key of an object.

A key the object does not own, or a member of anything that is not an object, is null. length on a string or an array is its length, counting UTF-16 code units for a string.

ExpressionResult
player.stats.str12
nothing.anythingnull
effects.length2

a[i] reads an array element, a string’s UTF-16 code unit, or an object key.

A whole number indexes an array or a string, counting from the end when negative; a fractional one is null. A number on an object reads the key str of it spells, so o[1.5] reads "1.5". A string reads an object key or length; on an array it reads the element its JavaScript Number value names ("1", " 1 ", and "0x1" all read element 1, a blank string reads element 0, and a negative one is null). Anything out of range or not owned is null.

ExpressionResult
effects[1]"slow"
effects[-1]"slow"
player['name']"Hero"

name(args) calls a builtin.

Only a builtin can be called, and only by name: an identifier in call position is never looked up in the bindings, and a binding value is never callable. An unknown name or a wrong argument count is a parse error.

ExpressionResult
round(hp / 3)12

true when the operand is falsy, false otherwise.

Always returns a boolean.

ExpressionResult
!deadtrue
!emptyfalse

The numeric negation of the operand.

Defined on numbers only; anything else is null.

ExpressionResult
-hp-35

The operand unchanged when it is a number.

Does not coerce: +'3' is null. Use num to parse a string.

ExpressionResult
+hp35
+'3'null

Numeric multiplication.

Defined on numbers only; anything else is null, and a non-finite result is null.

ExpressionResult
hp * 270

Numeric division.

Defined on numbers only. Division by zero is null, not Infinity or NaN.

ExpressionResult
hp / maxHp0.35
hp / zeronull

The remainder of numeric division, taking the sign of the dividend.

Defined on numbers only. A remainder by zero is null.

ExpressionResult
hp % 105
hp % zeronull

Numeric addition, or string concatenation when either side is a string.

Two numbers add. When either side is a string and the other is a primitive, both render with str and concatenate. Anything else, including an array or object operand, is null.

ExpressionResult
hp + 540
'HP: ' + hp"HP: 35"
effects + 'x'null

Numeric subtraction.

Defined on numbers only; anything else is null.

ExpressionResult
maxHp - hp65

Whether a orders before b.

Defined on two numbers or two strings; strings order by UTF-16 code unit, as JavaScript’s < does. Mixed types are false, not a coercion.

ExpressionResult
hp < maxHptrue
hp < '50'false

Whether a orders before or equal to b.

Defined on two numbers or two strings; strings order by UTF-16 code unit, as JavaScript’s < does. Mixed types are false.

ExpressionResult
hp <= 35true

Whether a orders after b.

Defined on two numbers or two strings; strings order by UTF-16 code unit, as JavaScript’s < does. Mixed types are false.

ExpressionResult
'b' > 'a'true

Whether a orders after or equal to b.

Defined on two numbers or two strings; strings order by UTF-16 code unit, as JavaScript’s < does. Mixed types are false.

ExpressionResult
score >= 7true

Strict equality between two primitives.

No coercion: 1 === '1' is false. Two arrays or two objects are never equal, even to themselves, because XEG has no identity to compare.

ExpressionResult
name === 'Hero'true
effects === effectsfalse

Another spelling of ===.

Accepted so existing data-if bodies keep working. It is strict: 1 == '1' is false.

ExpressionResult
score == '7'false

The negation of ===.

Two arrays or two objects are always unequal.

ExpressionResult
name !== 'Villain'true

Another spelling of !==.

It is strict: 1 != '1' is true.

ExpressionResult
score != '7'true

a && b is a when a is falsy, otherwise b.

Returns one of its operands, not a boolean, and does not evaluate b when a is falsy.

ExpressionResult
alive && hp35

a || b is a when a is truthy, otherwise b.

Returns one of its operands, not a boolean, and does not evaluate b when a is truthy.

ExpressionResult
blank || 'anonymous'"anonymous"

a ?? b is a unless a is null, in which case it is b.

Only null falls through. 0, false, and the empty string are kept, which is what separates it from ||. A failed computation is null, so ?? also supplies a fallback for one.

ExpressionResult
nothing ?? 'none'"none"
zero ?? 50

test ? a : b evaluates to a when test is truthy and b otherwise.

Selects a value, never a branch of markup. Only the chosen side is evaluated.

ExpressionResult
hp < 50 ? 'hurt' : 'fine'"hurt"

null, false, 0, and the empty string are falsy. Everything else is truthy, including an empty array and an empty object.

Any evaluation failure yields null: a type mismatch, a missing key, an index out of range, a division by zero, or a non-finite result. null is falsy for data-if and renders as the empty string for data-bind and interpolation.

An identifier resolves only against the fill’s declared bindings and, inside a data-each template, the names that template binds. An undeclared identifier is a validate-time error and null at runtime. Nothing reaches a host global.

Numbers (42, 3.5, .5, 2e3; a decimal point is always followed by a digit, and a literal that opens with its point takes no exponent), strings in single or double quotes with the escapes \n, \t, \r, \b, \f, \0, and \uXXXX, true, false, and null. There are no array, object, template, or regular-expression literals.

No assignment, increment, comma operator, arrow function, function, new, typeof, in, instanceof, delete, optional chaining, bitwise operator, or method call on a value. Every expression is a total, pure function of its bindings.

AttributeValueSummary
data-ifexpressionShows the node when the expression is truthy and hides it when it is falsy.
data-bindexpressionWrites the expression’s value into the node: its text, or the prop the vocabulary names with bindTo.
data-eachrepeat headerRenders the node once per element of an array.
data-keyexpressionNames each repeated copy’s identity, so an update patches copies by key instead of replacing them.
{{ }}expressionInterpolates an expression’s value into a text node.

The result is reported to the host in conditions, keyed by the node’s position, and the host hides the node by whatever means its renderer has. The tree is never pruned. An expression that fails or does not parse is false.

<div data-if="hp < 50">You're hurting!</div>

The value renders with str, so null writes the empty string. A bare binding name is writable: an input or change handler can push a new value back through the binding’s path, and a name whose binding is absent writes nothing so the authored fallback survives. Any other expression is read-only, and a write-back handler aimed at one is a lint error.

<span data-bind="hp">0</span>
<span data-bind="round(hp / maxHp * 100)">0</span>

The header is item in expr or (item, index) in expr. The expression is evaluated in the enclosing scope and must yield an array; anything else renders zero copies. Each copy has the item, the optional index name, and $index in scope for every expression inside it, shadowing bindings of the same name. Repeat names are values with no host path, so they are not writable. One render produces at most 10,000 copies across every repeat.

<li data-each="effect in effects"><span data-bind="effect"></span></li>
<li data-each="(member, i) in party" data-key="member.id">{{ i + 1 }}. {{ member.name }}</li>

Evaluated per copy with the item in scope. A key must be a string or a finite number, and 1 and "1" are different keys. A duplicate or invalid key drops the whole repeat to positional keying and reports a diagnostic. On a node without data-each it does nothing, and lint warns.

<li data-each="member in party" data-key="member.id"></li>

{{ }}

Active only in a fill that sets "interpolate": true, and only in text nodes; an attribute value is never an interpolation site. The value renders with str and stays text, so it can never become markup. \{{ writes a literal {{.

<p>Health: {{ hp }}/{{ maxHp }}</p>

The zero-based position of the current copy inside a data-each template. Bound in every copy whether or not the header names an index. Inside nested repeats it refers to the innermost one.