Templates and Expressions
Templates
A template is a string literal that contains zero or more
substitutions, where a substitution is a script or expression embedded
inside {
and }
.
The way that we interpret a template inside of an expression or script
is by placing the template between characters, also
called a template string:
`The result of 1 + 1 is ${1 + 1}.`
Expressions
An expression is a value composed, recursively, of one or more values, joined by a combination of operators and parenthesized subexpressions.
add(1, (i < 2 ? 1 : i)) + "x"
Expressions may be used to construct Adaptive Script statements and may be also used directly by Adaptive Framework when a value needs to be computed.
Before moving onto Statements, it's worth noting that Expressions are quite capable of being used to compute some complicated logic by nesting expressions in a functional-looking manner. For example, take the following expression:
add( if(1 < 2, 1, 2), 3 )
This expression says to add 1 to 3 if 1 is less than 2, and to add 2 to 3 otherwise. A more readable way to express this logic is using the syntactic sugar provided by statements.
Values
Values are the fundamental components of an expression. They can be List Values, Object Values, Scalar Literals, Evaluations, Parenthesized Expressions or Template Strings.
42
"abc"
true
null
[1, 2, 3]
{ "x": 42, "y": "abc" }
(1 + 1) + 3
`1 + 1 is {1 + 1}`
Factors
Factors join values with zero or more mathematical operators,
optionally separated with parentheses. This includes multiplication
*
, division /
, addition
+
, subtraction -
,
exponentiation **
, modulus %
and unary negation -
.
42 * 2
42 / 2
37 - 5
2 ** 8
32 % 6
-14
Comparisons
Comparisons join factors with zero or more relational operators,
optionally separated with parentheses. This includes less than
<
, less than or equal to
<=
, greater than >
,
greater than or equal to >=
, equal
==
, not equal !=
, strict
equal ===
and not strict equal
!==
. Comparisons result in a boolean result when
evaluated.
42 < 2
2 >= 2
1 !== 0
a === b
Logical Expressions
Logical Expressions are expressions joined together by the logical
operators &&
(and),
||
(or), and !
(not).
(a || b) && (c || !d)
Nullish Coalescing
A Nullish Coalescing expression joins two Logical Expressions using
the ??
operator. The result is the first
expression if it is not null or undefined, otherwise the result is
the second expression.
a ?? b