17. Functions

nexl allows to call JavaScript functions directly from the expression.

Let's create a simple JavaScript function:
function greeting() {
    return 'Hello, World !';
}
The following expression calls that function:
${greeting()}

There are two ways to pass parameters to the function.
The first one is via nested nexl expressions and second one is via function arguments buffer.

Let's say we have a function which joins two strings:
function join(str1, str2) {
	return str1 + ',' + str2;
}
The following expression calls that function and passes parameters via nested nexl expression:
${join( ${myStr1}, ${myStr2} )}
myStr1 and myStr2 are JavaScript variables declared somewhere in the JavaScript file.

The following statement won't work because each function parameter must be represented as nexl expression:
${join( myStr1, myStr2 )}
You have to use nexl expression between parentheses to pass a parameters.

The second method to call a function with parameters is to put parameters to the buffer in advance and then to call a function.
To put any value to the buffer use a | operation:
${myStr1|myStr2|join()}
This expression first resolves a myStr1 value and puts it to the buffer.
Next it does same with a myStr2 value.
And finally it calls a join() function which collects those values from the buffer and uses them for parameters.

Sometimes we need to pass a constant value to the function.
The following expression produces the 'Hello' string value:
${@Hello}

Next we can apply it as function parameters:
${join( ${@Hello}, ${@World} )}

We can do the same in the following way:
${@Hello|@World|join()}

nexl provides a lot of functions which can be useful in expressions.
For example the concat() function joins strings, arrays and merges objects.
See full functions list in the next section.

Watch a demo