> ## Documentation Index
> Fetch the complete documentation index at: https://docs.exoid.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Scripts

> Run JavaScript on a block to set a variable, flag a respondent or prepare a value from earlier answers.

Scripts are how you compute something the interview can't express with conditions alone: set a variable from a combination of earlier answers, flag a respondent who matches a profile you care about, prepare a value that a later question or a webhook will use. You write a few lines of JavaScript on a single block, and they run for every respondent who passes through it.

The **Script** button sits in the question panel of the block. The modal that opens says what it does: "Run JavaScript when a node opens or after a respondent submits an answer".

## The two tabs

<Tabs>
  <Tab title="On enter">
    Runs **before the node is shown**. Use it to prepare something the respondent is about to see or to set a value that depends on where they've been so far.
  </Tab>

  <Tab title="After submit">
    Runs **after the respondent submits their answer** on this block. Use it to turn the answer they just gave into a variable.
  </Tab>
</Tabs>

Each tab has its own code editor and its own script. A block can have both, one, or neither.

## What a script can read and write

Two objects are available inside a script.

<ParamField path="questions.<QUESTION CODE>.responseValue" type="read">
  The answer given to a question, addressed by its question code. This is why the question code is worth setting deliberately: it's the handle your scripts use.
</ParamField>

<ParamField path="vars.<name>" type="write">
  A variable of the campaign. Assign to it and the value stays with the respondent for the rest of the interview.
</ParamField>

The example loaded in the product shows both in two lines: read two answers, and write one variable when either matches.

```js theme={"dark"}
const shouldFlag = questions.Q1.responseValue === "option_1" || questions.Q2.responseValue === "yes";
if (shouldFlag) vars.flag = true;
```

<Warning>
  The full API reference isn't published yet. Stick to `questions.<CODE>.responseValue` for reading and `vars.<name>` for writing — those are the two documented entry points.
</Warning>

## Quick run

**Quick run** executes the script in an isolated state, without saving anything. It's the way to check that your code does what you think before any respondent goes through the block. Nothing you run this way touches the collected data or the variables of a real session.

<Tip>
  Set the question codes before you write the script. Renaming a code later breaks every script that referred to the old one, silently.
</Tip>

## Next steps

<CardGroup cols={2}>
  <Card title="Context variables" icon="braces" href="/en/logic/variables">
    Create the variables your scripts write to, and reuse them in question copy.
  </Card>

  <Card title="Logic and conditions" icon="git-branch" href="/en/logic/conditions">
    Show or skip a question, and branch the flow with navigation rules.
  </Card>

  <Card title="Webhooks" icon="webhook" href="/en/webhooks/introduction">
    Send each response, variables included, to your own endpoint as it comes in.
  </Card>
</CardGroup>


## Related topics

- [Block options](/en/builder/block-options.md)
- [Context variables](/en/logic/variables.md)
- [Logic and conditions](/en/logic/conditions.md)
