Supported JavaScript functions
The following are supported JavaScript functions with code examples. You can use these when creating a condition.
Array destructuring
Sitecore CDP supports the array destructuring feature. You can use the following JavaScript to unpack values from an array:
var [a, ...b] = [1, 2, 3];
print(a); // 1
print(b); // [2, 3]Array filter/map/reduce
Sitecore CDP 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:
const getMax = ( max, cur ) => Math.max( max, cur );
print(
[ { x: 10 }, { x: 66 }, { x: 150 } ]
.filter(el => el.x < 100)
.map( el => el.x )
.reduce( getMax, -Infinity )
);
// expected output: 66Arrow functions
Sitecore CDP 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:
let materials = [
'Hydrogen',
'Helium'
];
print(materials.map(material => material.length));
// expected output: Array [8, 6]Classes
Sitecore CDP 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:
let Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
print(Rectangle.name);
// output: ""Rectangle""Default function parameters
Sitecore CDP 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:
function multiply(a, b = 2) {
return a * b;
}
print(multiply(5));
// expected output: 10Let and const bindings
Sitecore CDP 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:
const foo = 'bar';
let ten = 10;
print(foo, ten);
// expected output: bar, 10Object destructuring
Sitecore CDP supports object destructuring. Use this to extract properties from objects and bind them to variables.
The following JavaScript includes an example:
var o = {p: 42, q: true};
var {p, q} = o;
print(p); // 42
print(q); // trueRest parameters
Sitecore CDP 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:
function sum(...theArgs) {
return theArgs.reduce((previous, current) => {
return previous + current;
});
}
print(sum(1, 2, 3));
// expected output: 6Spread syntax
Sitecore CDP 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:
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
print(sum(...numbers));
// expected output: 6Template literals
Sitecore CDP 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:
const a = 5;
const b = 10;
print(`Fifteen is ${a + b} and not ${2 * a + b}.`);
// ""Fifteen is 15 and not 20.""