<let> vs <const>

TL;DR
  • <let>: mutable reactive state
  • <const>: derived value, pure, recomputes with dependencies
  • static const: module-level constant, initialized once, not reactive

A .marko template has three ways to bind a variable, each signaling how the value updates: <let> holds mutable state whose reassignment triggers reactive updates, <const> computes a derived value whenever its dependencies change, and static const evaluates once at module load for values that never vary across instances.

Mutable State

<let> declares state written directly, typically from event handlers. Assigning to its tag variable updates every expression that reads it:

<let/tipPercent=20>

<button onClick() { tipPercent = 18 }>18%</button>
<button onClick() { tipPercent = 22 }>22%</button>

<output>Tip: ${tipPercent}%</output>
let/tipPercent=20

button onClick() { tipPercent = 18 } -- 18%
button onClick() { tipPercent = 22 } -- 22%

output -- Tip: ${tipPercent}%

Clicking a button reassigns tipPercent, updating only the <output> text.

The value= attribute sets only the initial value. Afterward, an uncontrolled <let> changes solely through assignment (or a valueChange handler), even if the initializing expression updates.

Derived Values

<const> computes a value from other reactive variables. It cannot be reassigned and recomputes automatically whenever its dependencies change:

<let/bill=64>
<let/tipPercent=20>
<const/tip=bill * (tipPercent / 100)>

<button onClick() { tipPercent += 1 }>Round up</button>

<output>Tip: $${tip.toFixed(2)}</output>
let/bill=64
let/tipPercent=20
const/tip=bill * (tipPercent / 100)

button onClick() { tipPercent += 1 } -- Round up

output -- Tip: $${tip.toFixed(2)}

tip always stays in sync with bill and tipPercent from a single declaration.

Warning

Mirroring a derivation into a <let> and updating it manually is an anti-pattern:

<let/bill=64>
<let/tipPercent=20>
<let/tip=bill * (tipPercent / 100)>

<button onClick() {
  tipPercent += 1;
  tip = bill * (tipPercent / 100); // ❌ repeated in every handler
}>
  Round up
</button>
let/bill=64
let/tipPercent=20
let/tip=bill * (tipPercent / 100)

button onClick() {
  tipPercent += 1;
  tip = bill * (tipPercent / 100); // ❌ repeated in every handler
} --
  Round up

Every handler touching bill or tipPercent would have to recompute tip or leave it stale.

Because updates are batched, a derived <const> read inside an event handler reflects the previous state until the queue flushes. See Stale Derived Values.

Module Constants

Statements prefixed with static execute in module scope once per environment when the template loads, shared across every instance and request:

static const currencyFormat = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
});

<let/bill=64>
<let/tipPercent=20>
<const/total=bill * (1 + tipPercent / 100)>

<output>${currencyFormat.format(total)}</output>
static const currencyFormat = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
});

let/bill=64
let/tipPercent=20
const/total=bill * (1 + tipPercent / 100)

output -- ${currencyFormat.format(total)}

currencyFormat is constructed once regardless of how many components render, making static const ideal for formatters, lookup tables, configuration, and helper functions. Static statements sit outside the reactive system; anything reading input or a tag variable belongs in <const> or <let>.

Note

Even a value that never changes after mount must use <const> if it reads input or a tag variable, so it initializes per instance.

Choosing

Choose the narrowest declaration that fits the value:

  • Reassigned by handlers or mutable over time: <let>
  • Computed from input or other reactive values: <const>
  • Shared identically across every instance: static const

Beyond clarity, targeted compilation generates browser update code only for values that can change, so narrower declarations produce less client work.


Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.