---
title: Macros
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/macros/
updated: 2026-11-18T16:57:44.579Z
---

# Macros

> NEWMACRO() and the function it returns.

## API

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

Type: `method`

Compiles a simple macro script into a JavaScript function.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `str` | `String` | yes |  | The macro source. |
| `nocompile` | `Boolean` | no | `false` | Returns the generated JavaScript code as a string. |
| `isasync` | `Boolean` | no | `false` | Creates an async function. |

Returns: `Function|String` A `Function(model, helpers)`, or the code with `nocompile`.

Macros let users write small rules without JavaScript. Supported: `IF`, `ELSE IF`, `ELSE`, `FI`, `RETURN`, `AND`, `OR`, `TRUE`/`FALSE`/`YES`/`NO`/`OK`, `NULL`, quoted values, helper calls `NAME(args)`, private variables `#name` and comments `//`. Keywords are case-insensitive. Model properties and helper names are converted to lower case, so keys in the model and helpers must be lower case. Avoid property names that contain `if` or `else` (for example `verified`), because the compiler replaces those letters as keywords. More statements can be on one line separated by `;`.

The compiled function has the signature `fn(model, helpers)`; helpers run with the model as `this`. With `isasync` every helper call is awaited and the function returns a Promise.

Example: Price rule
```js
var fn = NEWMACRO('IF price > 100\n\tRETURN "expensive"\nFI\nRETURN discount(price)');
fn({ price: 200 }, {}); // expensive
fn({ price: 50 }, { discount: function(price) { return price * 0.9; } }); // 45
```

Example: Async helpers
```js
var fn = NEWMACRO('#total = price * count\nRETURN rate(#total)', false, true);
var value = await fn({ price: 2, count: 3 }, { rate: async function(val) { return val * 1.2; } });
```

### fn(model, [helpers])

Type: `delegate`

The function returned by `NEWMACRO()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `model` | `Object` | yes |  | The data. |
| `helpers` | `Object` | no |  | Functions used by the macro. |

Returns: `Object|Promise` The value of `RETURN`.

`model` gives the values of properties; `helpers` is an object of functions called by name from the macro. Private `#variables` live only during one call.

Example: Run
```js
var check = NEWMACRO('IF age >= 18 AND active\nRETURN TRUE\nFI\nRETURN FALSE');
check({ age: 20, active: true }); // true
```
