# Variable operation

Writes to a variable a value you set yourself or the result of a calculation. The block suits counters, points in tests and quizzes, and calculating a total.

![Settings panel of the "Variable operation" block](https://support.watbot.org/_media/en/01-chatbots/03-blocks/04-contact-data/_images/variable-operation.webp?v=1791781230)

## Settings

- **"Type of operation"** — what exactly the block does:
  - "Arbitrary value" — write the given value to the variable;
  - "Mathematical expression" — change one variable: add, subtract, multiply, divide;
  - "Mathematical expression 2.0" — calculate an expression with several variables;
  - "Mass assignment" — write values to several variables at once.
- **"Value/Expression"** — what to write or what to calculate. How to fill in the field for each type is described below.
- **"Result variable name"** — the variable the result goes to.
- **"Result variable type"** — "Local variable" (each contact has their own) or "Global variable" (shared by the whole bot).

The "Mass assignment" type has none of these three fields — the "Variables to assign" list takes their place.

### Arbitrary value

Type text or a number in the "Value/Expression" field. [Templates](https://support.watbot.org/chatbots/templates) work here, including [expressions](https://support.watbot.org/chatbots/templates/expressions), — so you can build the value from other variables and contact data.

!["Variable operation" block: the order total is written to a variable](https://support.watbot.org/_media/en/01-chatbots/03-blocks/04-contact-data/_images/variable-operation-free.webp?v=1791781230)

### Mathematical expression

Calculates the variable's new value from its current value. In the expression the variable is always written as `x` — whatever name you gave in the "Result variable name" field. For example, `x + 1` increases the variable by one.

- **"Initial value of the variable"** — if the variable did not exist before this block, the calculation starts from this number.

!["Variable operation" block: a 10% discount on the order total with the expression x * 0.9](https://support.watbot.org/_media/en/01-chatbots/03-blocks/04-contact-data/_images/variable-operation-math.webp?v=1791781230)

### Mathematical expression 2.0

The expression can use several variables. In the **"Variables for Expression"** list, click "Local variable" or "Global variable" and enter the variable name. Each one gets a short name: the first — `x`, the second — `y`, the third — `z` and so on. These short names are what you write in the expression, for example `(x + y) * 10`.

If one of the variables does not exist yet, 0 is used instead.

!["Variable operation" block: the discount is calculated from the order total and the discount percentage](https://support.watbot.org/_media/en/01-chatbots/03-blocks/04-contact-data/_images/variable-operation-math2.webp?v=1791781230)

### What you can write in a mathematical expression

- Numbers, the `+`, `-`, `*`, `/` signs and brackets.
- Fractional numbers — with a dot: `1.24`.
- Rounding up to a whole number: wrap the whole expression in `ceil( )`, for example `ceil(x / 3)`.

`{{ }}` templates do not work in mathematical expressions — use the short variable names. The bot removes everything from the expression except numbers, signs and short names.

### Mass assignment

In the **"Variables to assign"** list, click "Local variable" or "Global variable", enter the variable name and its value. You can add several rows — the block writes all the values at once. [Templates](https://support.watbot.org/chatbots/templates) work in the values, but only with simple functions.

!["Variable operation" block: four order variables are set at once](https://support.watbot.org/_media/en/01-chatbots/03-blocks/04-contact-data/_images/variable-operation-mass.webp?v=1791781230)

## Outputs

- "Continue" — the bot moves on right after saving.

## Good to know

- If a variable with this name does not exist yet, the block creates it.
- The result is saved as text. You can insert it into a message like any other variable.
- If the mathematical expression has an error — for example, division by zero, or the variable holds text instead of a number — an error message is written to the variable. Check the expression in the bot preview.
