---
title: Macros
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/macros/
updated: 2026-11-18T16:56:54.572Z
---

# Macros

> NEWMACRO() and the macro syntax.

## API

### NEWMACRO(code, [nocompile], [isasync])

Type: `method`

Compiles a macro into a function `fn(model, helpers)`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `code` | `String` | yes |  | The macro, one command per line (or separated by `;`). |
| `nocompile` | `Boolean` | no | `false` | Returns the JavaScript code instead of a function. |
| `isasync` | `Boolean` | no | `false` | Makes an async function. |

Returns: `Function` `function(model, [helpers])`, or a string with `nocompile`.

The function returns the value of the first `RETURN` that runs, or `undefined`. Words are read from `model` and `name(...)` calls `helpers.name` with the model as `this`. With `isasync` the function is async and every helper call is awaited. With `nocompile` you get the generated JavaScript as a string, which is useful to check a rule.

Example: Shipping
```js
var shipping = NEWMACRO('IF weight > 10\n\tRETURN 15\nELSE IF weight > 2\n\tRETURN 8\nFI\nRETURN 4');
shipping({ weight: 5 }); // 8
```

Example: Async helpers
```js
var fn = NEWMACRO('RETURN rate(currency) * price', false, true);
var value = await fn({ price: 10, currency: 'EUR' }, { rate: async function(code) { return 1.1; } });
```

### Macro syntax

Type: `type`

Commands and values of `NEWMACRO()`.

| Syntax | Meaning |
| --- | --- |
| `IF condition` … `ELSE IF condition` … `ELSE` … `FI` | Conditions; `FI` closes them |
| `RETURN expression` | Returns a value |
| `AND`, `OR` | `&&`, `\|\|` |
| `==`, `!=`, `>`, `>=`, `<`, `<=`, `+`, `-`, `*`, `/` | Operators as in JavaScript |
| `price`, `order.total` | Properties of the model, read in lower case |
| `#name` | A temporary variable: `#total = price * count` |
| `name(a, b)` | A helper call |
| `"text"`, `'text'` | Text; a quoted number becomes a number |
| `TRUE`, `FALSE`, `YES`, `NO`, `NULL` | Values |
| `// text` | A comment line |

Keywords can be in any case.

Example: Rule
```js
IF total >= 100 AND customer.vip == YES
	RETURN total * 0.9
FI
RETURN total
```
