This article mentions details of JEXL.
Operation | Symbol |
---|---|
Negate | ! |
Example
Below code means false by negate operation.
{{!true}}
Operation | Symbol |
---|---|
Add, Concat | + |
Subtract | - |
Multiply | * |
Divide | / |
Divide and floor | // |
Modulus | % |
Power of | ^ |
Logical AND | && |
Logical OR | || |
Comparison | Symbol |
---|---|
Equal | == |
Not equal | != |
Greater than | > |
Greater than or equal | >= |
Less than | < |
Less than or equal | <= |
Element in array or string | in |
A note about in
The in
operator can be used to check for a substring:
"Cad" in "Ron Cadillac"
, and it can be used to check for an array element:
"coarse" in ['fine', 'medium', 'coarse']
. However, the ==
operator is used
behind-the-scenes to search arrays, so it should not be used with arrays of
objects. The following expression returns false: {a: 'b'} in [{a: 'b'}]
.
Conditional expressions check to see if the first segment evaluates to a truthy value. If so, the consequent segment is evaluated. Otherwise, the alternate is. If the consequent section is missing, the test result itself will be used instead.
Expression | Result |
---|---|
"" ? "Full" : "Empty" | Empty |
"foo" in "foobar" ? "Yes" : "No" | Yes |
{agent: "Archer"}.agent ?: "Kane" | Archer |
Type | Examples |
---|---|
Booleans | true , false |
Strings | "Hello "user"", 'Hey there!' |
Numerics | 6, -7.2, 5, -3.14159 |
Objects | {hello: "world!"} |
Arrays | ['hello', 'world!'] |
Parentheses work just how you'd expect them to:
Expression | Result |
---|---|
(83 + 1) / 2 | 42 |
1 < 3 && (4 > 2 || 2 > 4) | true |
Access variables in the context object by just typing their name. Objects can be traversed with dot notation, or by using brackets to traverse to a dynamic property name.
Example context:
{
name: {
first: "Malory",
last: "Archer"
},
exes: [
"Nikolai Jakov",
"Len Trexler",
"Burt Reynolds"
],
lastEx: 2
}
Expression | Result |
---|---|
name.first | Malory |
name['la' + 'st'] | Archer |
exes[2] | Burt Reynolds |
exes[lastEx - 1] | Len Trexler |
In JEXL, common built-in functions are provided, but also functions library is extended with lodash functions.
You can use lodash functions from this link.
Also you can check our functions from this article.
Example
Check JEXL codes below
Find Min
{{ min([65, 42, 87, 88]) }}
// => 42
Reverse Array
{{ reverse([1, 2, 3, 4]) }}
// => [4, 3, 2, 1]
Check Equal
{{ eq({ a:1 }, { a:1 }) }}
// => true
{{ eq(3, 4) }}
// => false
Add Elements To Array
{{ concat([1], 2, 3, 4) }}
// => [1, 2, 3, 4]
Get Last Element of Array
{{ reverse([1, 2, 3]) }}
// => 3