Introduction to functions

Introduction

Functions encapsulate logic into reusable chunks. Discover provides a range of predefined functions to simplify common tasks, so you don't need to be a JavaScript expert to take advantage of scripting. Functions can be used in survey elements, error messages, and the survey URL.

Anatomy of a function

A function has three parts:

  1. Name: The unique identifier for the logic the function contains.
  2. Parameters: The information the function needs to perform its operation. Some functions require no parameters, others require several. Parameters are passed in as a comma-separated list wrapped in parentheses. Each parameter has a type — passing in the wrong type will result in an error or unexpected behavior. Parameter types are listed after a colon following each parameter name.
  3. Return type: The type of data the function returns. Not all functions are supported in all survey areas, and using a function on an unintended element type may produce unexpected results.
Anatomy of A Function

Calling a function

Place the function name inside {{ and }} and replace the parameter names with the desired values.

For example, to show a respondent what they selected in a previous single-select question:

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

Would display as:

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