Supported JavaScript functions
The following are supported JavaScript functions with code examples. You can use these when writing server-side JavaScript for a programmable decision.
Array destructuring
Array destructuring
Sitecore Personalize supports the array destructuring feature. You can use the following JavaScript to unpack values from an array:
Array filter/map/reduce
Array filter/map/reduce
Sitecore Personalize supports the .map(), .reduce(), and .filter() methods.
The filter() method applies a conditional statement against each element in an array. If the condition returns true, the element is pushed to the output array.
The map() method creates a new array from an existing array and applies a function to each one of the elements of the first array.
The reduce() method runs a reducer function on each element of the array to reduce the array of values to just one.
The following JavaScript includes examples:
Arrow functions
Arrow functions
Sitecore Personalize supports the arrow function that lets you write a shorter function syntax. If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword.
The following JavaScript includes an example:
Classes
Classes
Sitecore Personalize supports class as a type of function. Instead of using the keyword function to initiate it, use the keyword class. The properties are assigned inside a constructor() method.
The following JavaScript includes an example:
Default function parameters
Default function parameters
Sitecore Personalize supports default function parameters. The default parameter enables you to set default values for function parameters if a value is not passed in.
The following JavaScript includes an example:
Let and const bindings
Let and const bindings
Sitecore Personalize supports let bindings and const bindings.
Let bindings are created at the top of the (block) scope containing the declaration.let variables and are not initialized until their definition is evaluated.
The const statement declares a local variable the same as the let statement. However, after it is initialized, it cannot be reassigned with any other value. It must be initialized when declared.
The following JavaScript includes examples:
Object destructuring
Object destructuring
Sitecore Personalize supports object destructuring. Use this to extract properties from objects and bind them to variables.
The following JavaScript includes an example:
Rest parameters
Rest parameters
Sitecore Personalize supports the rest parameter as an improved way to handle various input as parameters in a function. The rest parameter syntax enables you to represent an indefinite number of arguments as an array.
The following JavaScript includes an example:
Spread syntax
Spread syntax
Sitecore Personalize supports the spread syntax (...), which allows an iterable to expand in places where zero or more arguments are expected.
The following JavaScript includes an example:
Template literals
Template literals
Sitecore Personalize supports the template literal syntax, which enables you to use backticks (`) instead of single (') or double (") quotes when working with strings.
The following JavaScript includes an example: