Introduction to functions

Functions are ways of encapsulating logic into reusable chunks. Designed to simplify tasks and eliminate the requirement of being a JavaScript expert to harness the potential of scripting, Discover offers a range of predefined functions that you can integrate into your scripts.

These functions can be used in areas such as survey elements, error messages, or the survey URL.

Anatomy of a function

A function has three parts:

  1. Name: the unique identifier for the block of logic contained in the function.
  2. Parameters: the information that the function needs to perform its operation. Some functions may not require any parameters while others may require several. Parameters are passed in as a comma separated list wrapped in parenthesis.
    1. Each parameter also has a type. Passing in the wrong type of data to the function when calling it will result in an error or unexpected behavior. The parameter types are listed after a colon following each parameter.
  3. Return type: not all operations are supported for all types of survey elements and some operations may behave unexpectedly when performed on an unintended type, so it is important to know what kind of data the function will return to you.
Anatomy of A Function

Calling a function

After selecting a function, simply put the name of the function into the script ({{ and }}) and replace the names of the parameters with the desired values.

For example, if you wanted to use the above function to show to the respondent what they selected in the previous single select question, you could add the following script to the question text of a follow-up question:

In the last question, you said {{ GetLabel("Color Question") }} was your favorite. Tell us why it's your favorite color.

This would then return the following to the respondent:

In the last question, you said blue was your favorite. Tell us why it's your favorite color.