Variable logic

Introduction

Variable logic allows you to evaluate conditional logic and save values to predefined variables in your survey.

Adding variable logic is easy, just:

  1. Click any Add button.
  2. Select Variable logic from the list to add it into your survey flow.
  3. Configure settings as needed.
  1. Click any Add button.
    The survey authoring interface with the add buttons highlighted.
  2. Select Variable logic from the list to add it into your survey flow.
    Add Menu Variable Logic element
  3. Select variable or create a new one.
  4. Configure the Variable logic settings.
    Variable Logic Element

When a respondent arrives at the point in the survey where the variable logic is located, the logic is evaluated and if the condition is true, then the provided value is saved to the database.

Variable logic elements are not seen by respondents. Instead, the operation occurs behind the scenes.

Writing variable logic & setting values

Variable logic is composed of a few pieces:

  1. Statements
  2. Conditions
  3. Set-value input
Variable Logic Element Anatomy

Logic statements and Conditions function the same as in Skip logic. For detailed instructions, refer to the Skip logic documentation.

As a quick overview, use the dropdown interface to build conditions that are evaluated when the variable logic element is encountered in the survey.

  1. Select an existing variable, or create a new one, to save values to (variables are also found in the variable manager under tools). Upon selection the variable is displayed in the logic statement header. 
    Note that unique ID and URL variables are not available in variable logic. For more info about variable types, see the Variable manager documentation.
  2. Define the condition using the dropdown interface.
  3. At the end of each statement, define the value to save to the database for respondents whose condition evaluates as true.

Example

Suppose you want to segment respondents into two categories, "State Side" or "Foreign," based on their answers to a Country question. The logic might look like this:

  • Statement1:
    • If Country = 1: United States
    • Then sent variable location to “State Side”
  • Statement 2:
    • If Country = 2: Canada, 3: Mexico, or 4: Brazil
    • Then sent variable location to “Foreign”
Variable Logic Example (stateside)

Advanced logic and scripting

Using the {{ }} notation in the value input field, you can define scripting blocks to perform advanced operations with JavaScript.

For example, to calculate total household income based on values from two numeric questions: income1 and income2. In the value input, use the {{ }} notation to start a scripting block and reference the two income questions with the getValue() function, then join them with the add (+) operator like so:

{{ getValue(“income1”) + getValue(income2) }}

This script sums the responses to the two income questions and saves the result to the selected variable when the specified condition in the dropdown interface evaluates to true.

Example of advanced logic using scripting in the set-value input box

Use the Insert Variable button below the value input box to quickly add scripting notation and survey variables with the correct getValue() syntax.