Calculate values with JavaScript

Use a JavaScript step in Flow to calculate a score, create a label, or decide whether someone qualifies for a later branch. The code runs in the participant’s browser when they reach that step.

You only need basic JavaScript for these examples. For fixed values or simple counters, use Set variable instead.

The examples below use fixed inputs to show where code goes and how its results are saved. To calculate from a participant's response, follow Calculate from a participant's answer.

Add your first calculation

  1. In Flow, select the circled + where the calculation should run, then JavaScript.
  2. Enter a name under Save result as and choose Result contains: Number, Text, or Yes / no.
  3. Under Calculate with JavaScript, use return followed by the value to save, as in the examples below. Do not add a function wrapper.
  4. Use Preview to test it. Changes save automatically.

A result becomes available to later Flow steps. You can use it in an If condition, read it in another calculation, or insert it into question text with the placeholder menu. Completed responses include the result in Results and exports.

Example 1: double an input

First add a Set variable step before the JavaScript step. Set price to the Number 10.

In the JavaScript step, set Save result as to double_price, choose Number, and enter:

const price = variables.get("price");
return price * 2;

The input is 10; the saved result is 20. variables.get("price") reads the earlier value. return saves the answer under double_price.

To see it, add a later page in Content with an Information element. In Information text, type Doubled price: , choose Insert placeholder, and select double_price. In Flow, place this page after the JavaScript step. Open Preview and continue to that page: it should display Doubled price: 20.

Example 2: read two inputs and save two results

Add a second JavaScript step after Example 1’s output page. Before this new step, add two Set variable steps: base_score as Number 8, and bonus as Number 3.

In this new JavaScript step, configure the first calculation as Save result as total_score, Result contains Number:

const base = variables.get("base_score");
const bonus = variables.get("bonus");
return base + bonus;

Select Add calculation. Configure the second calculation as Save result as eligible, Result contains Yes / no:

const base = variables.get("base_score");
const bonus = variables.get("bonus");
return base + bonus >= 10;

This saves total_score = 11 and eligible = true. On a page after this step, add an Information element. In Information text, type Total: , use Insert placeholder to select total_score, type ; eligible: , and insert eligible. In Preview, continue to that page: it should display Total: 11; eligible: true.

You can also use an If step after this node to show a follow-up page when eligible is true.

Calculations in the same step start together and read the values available when that step begins. The second calculation therefore reads the original inputs again. If it needs the newly saved total_score, put it in a following JavaScript step.

Run a calculation without a result name

Leave Save result as empty to run code without saving its return value. The return value is ignored, and Result contains does not constrain it. You can omit return entirely. Save every Flow output explicitly with variables.set("name", value).

The output names and types still need declarations in the survey. For example, add a Set variable step that sets score to the Number 0, then add a JavaScript step with Save result as empty:

variables.set("score", 12);

The saved score is 12. You can write any declared Flow variable, including one declared in another step. URL parameters, study assignments, study fields, and the browser scanner value are read-only. An unnamed calculation adds no variable or result column of its own. See writing variables for the declaration rules.

Before publishing

Every calculation needs code. A named calculation must return the chosen type; an unnamed calculation ignores the return value and must save any Flow outputs with variables.set. Use names such as total_score: start with an English letter, then use letters, digits, or underscores, up to 64 characters. Names are case-sensitive.

If a calculation fails, none of that step’s result or global-data changes are saved. Preview shows which calculation failed. Check both the usual route and branches with missing optional answers.

Continue with reading variables and answers, saving additional data, question scripts and shared helpers, browser scanner results, or errors and limits.

Confirm action