The spread operator (...) makes it easier to build arrays, objects, and call functions where the list of arguments is built at runtime. For instance, while using in an array literal, the value on the right of the operator is inserted into the new array at that position. Similarly, in a function call, the array expands and is used as the arguments to the function. In an object literal, the right side of the operator is another object that has its keys added to the new object.
To build a new array that is made up of another array surrounded by two elements:
["header", ...$body, "footer"]
If you want to find the maximum value in an array of numbers:
To create a new object with some fixed values and entries in the "$extra" object:
The conditional operator (?) allows you to specify a value to use based on the result of an expression. The syntax is as follows:
condition ? trueValue : falseValue
The condition expression will be evaluated and the trueValue will be the result of the expression if the condition evaluates to true, otherwise the falseValue will be the result.
$text == "NFL" ? "foo" : "bar"
$counter > 1 ? ($counter < 3 ? 50 : 100) : -1
The instanceof operator returns True if the given object is an instance of the given type. The syntax is as follows:
object instanceof type
The possible values for the type are: Null, Boolean, String, Number, Object, Array, Date, LocalDate, DateTime, and LocalDateTime.
$my_array instanceof Array
The match operator allows you to conditionally evaluate one of a number of expressions based on whether an input value matches a given pattern. See the match Control Flow Operatorsection to learn more.
The typeof operator returns the type of a value as a string. The syntax is as follows:
The possible return values are: "boolean", "number", "string", "object", and "array".
The in operator returns true if the property name or array index is in the given object or array. The syntax is as follows:
name|index in operand
'name' in $
You can add notes to your expressions using comments. A comment starts with '/*' and ends with '*/', for example:
/* say hello */ "Hello, World!"
The comment will be ignored when evaluating the expression, it is only for the reader's benefit.
* / %
< <= > >=
Creating variables and assigning values is not supported
Custom expression language functions can be created using the arrow function syntax:
// A function that takes multiple parameters:
(param1, param2, ..., paramN) => expression
// A function that takes a single parameter does not need parentheses:
(param) => expression
param => expression
// A function with no parameters requires parentheses:
() => expression
// Function parameters can also have default values
(param1 = defaultValue1, param2 = defaultValue2, ..., paramN = defaultValueN) => expression