# Calculated values

> Expression and Default value, for computing field values from other fields.

Source: https://ngxviewbuilder.io/creators/calculated-values

Two Logic properties produce values instead of true/false.

## Expression

**Expression** continuously calculates the element's value. Whenever a referenced field changes, the result is recomputed and written into the field.

```text
{price} * {quantity}                            line total
{firstName} + " " + {lastName}                  full name
{isCompany} ? {companyCode} : {personalCode}    pick one of two codes
sumInArray({orderLines}, "total")               sum over table rows
```

The **Visual builder** tab can write this kind of expression too: give the conditions, then what to return when they match and what to return otherwise (a text, a number, Yes/No, another field's value, nothing, or a formula). See [Building a rule without writing it](https://ngxviewbuilder.io/creators/conditional-logic#building-a-rule-without-writing-it).

Rules of thumb:

- A field with an Expression is effectively computed, so combine it with **Read only** and users won't fight the recalculation.
- Never reference the field itself; self-referencing expressions are skipped with a console warning.
- On **Number** elements the result respects the element's formatting settings (fraction digits, locale).
- If the expression errors or returns nothing, the value is cleared.
- To land the result somewhere else instead of in this field, write it: `{variable1} = sumInArray({orderLines}, "total")`. See [Writing values back](https://ngxviewbuilder.io/creators/functions#writing-values-back).

## Default value

**Default value** sets the initial value once, on load. The user can overwrite it.

Three forms:

| Form | Example | Result |
| --- | --- | --- |
| Static text | `Draft` | literal "Draft" |
| Token | `{__variables.route.id}` | resolved at load |
| Expression (prefix `=`) | `=today()` | evaluated at load |

Common defaults:

```text
=today()                        today's date
{__external.userName}           current user from the host app
{__variables.route.clientId}    id from the URL
"LT"                            fixed country pre-selection
```

## Expression vs. Default value

| | Expression | Default value |
| --- | --- | --- |
| When it runs | continuously | once at load |
| User can override | no (recalculated) | yes |
| Use for | totals, derived fields | sensible starting values |

## Worked example: invoice line

| Element | Type | Setup |
| --- | --- | --- |
| `quantity` | Number | Default value `1` |
| `unitPrice` | Number | filled from a product data source |
| `discount` | Number | Default value `0` |
| `lineTotal` | Number, read-only | Expression: `{quantity} * {unitPrice} * (1 - {discount} / 100)` |
