# Incrementing Your Own Loop Variable

**URL:** <https://community.n8n.io/t/incrementing-your-own-loop-variable/79196>\
**Category:** Tips & Tricks\
**Created:** [February 18, 2025, 1:31pm UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196 "2025-02-18T13:31:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubschrauber](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/hubschrauber/32/46788_2.png) [@hubschrauber](https://community.n8n.io/u/hubschrauber)\
**Post date:** [February 18, 2025, 1:31pm UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196/1 "2025-02-18T13:31:17Z")

</div>

Controlling your own loop in n8n can get a little tricky because you have to use values from nodes that are in scope before the loop (initialized) AND within the loop (updated/incremented).

This makes use of the `node.isExecuted` built in variable, and a JS ternary operator, to decide whether to use the initial value or the value from the last iteration of the loop. Examine the expression in the `Increment Loop Var` node for details.

Key elements to making this work.

- All of the expressions **must** reference specific `Set` nodes by name (e.g. `$('').first().json.page`). Nothing can just reference an attribute of the input (immediately previous node) (i.e. NOT `$input.first().json.page` and NOT `$json.page`).
- The initial (first loop iteration) value must be set **outside** (previous to) the loop.
- There must be a **second** `Set` node within the loop that **forwards** the value back to the `Increment` `Set` node, preferably using a different name (`prevPage`) at the beginning of the loop, because the `Increment` node **can’t refer to itself** in an expression.
- The increment node’s expression must have the ternary conditional clause (i.e. `(test) ? valueIfTrue : valueIfFalse`) to **choose** whether to use the initial value, or increment the value (`prevPage`) forwarded from the other `Set` node within the loop.

---

<div class="post-metadata">

**Author:** ![hubschrauber](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/hubschrauber/32/46788_2.png) [@hubschrauber](https://community.n8n.io/u/hubschrauber)\
**Post date:** [February 18, 2025, 2:24pm UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196/2 "2025-02-18T14:24:08Z")

</div>

Pre-emptive reply to myself… I realize this can also be done in `Code` nodes by referencing [`$getWorkflowStaticData`](https://docs.n8n.io/code/cookbook/builtin/get-workflow-static-data/) but each `Code` node sets up a JS execution sandbox that can slow things down, so if they **can** be avoided, they probably **should** be.

---

<div class="post-metadata">

**Author:** ![Tero](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/tero/32/47987_2.png) [@Tero](https://community.n8n.io/u/Tero)\
**Post date:** [February 21, 2025, 3:53pm UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196/3 "2025-02-21T15:53:29Z")

</div>

This is pure genius. The example is really well built and helped me understand the concept without resorting to ChatGPT. Thank you for sharing! ❤

---

<div class="post-metadata">

**Author:** ![ENE\_5K4](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ene_5k4/32/60402_2.png) [@ENE\_5K4](https://community.n8n.io/u/ENE_5K4)\
**Post date:** [September 9, 2025, 10:53am UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196/4 "2025-09-09T10:53:59Z")

</div>

The simplest logic for iterating any value with a loop. It shouldn’t be just limited with pagination. Thank you 🤟

---

<div class="post-metadata">

**Author:** ![webwoodme](https://avatars.discourse-cdn.com/v4/letter/w/53a042/32.png) [@webwoodme](https://community.n8n.io/u/webwoodme)\
**Post date:** [January 3, 2026, 9:09pm UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196/5 "2026-01-03T21:09:02Z")

</div>

@[hubschrauber](http://community.n8n.io/u/hubschrauber)

Do you have an example with a merge node triggering loop ?  
I’m having an if condition that triggers a rebuild of one of the input, and it doesn’t trigger again when using 2 merge node.

---

<div class="post-metadata">

**Author:** ![webwoodme](https://avatars.discourse-cdn.com/v4/letter/w/53a042/32.png) [@webwoodme](https://community.n8n.io/u/webwoodme)\
**Post date:** [January 3, 2026, 9:11pm UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196/6 "2026-01-03T21:11:02Z")

</div>

(If you execute it you will see that it doesn’t repeat forever)

---

<div class="post-metadata">

**Author:** ![Parintele\_Damaskin](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/parintele_damaskin/32/139243_2.png) [@Parintele\_Damaskin](https://community.n8n.io/u/Parintele_Damaskin)\
**Post date:** [January 3, 2026, 9:37pm UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196/7 "2026-01-03T21:37:36Z")

</div>

Or simply use

> {{$runIndex + 1 }}

Or any other math operation.

It keep count how many times that respective node has runned i n the workflow.

---

<div class="post-metadata">

**Author:** ![webwoodme](https://avatars.discourse-cdn.com/v4/letter/w/53a042/32.png) [@webwoodme](https://community.n8n.io/u/webwoodme)\
**Post date:** [January 3, 2026, 10:18pm UTC](https://community.n8n.io/t/incrementing-your-own-loop-variable/79196/8 "2026-01-03T22:18:35Z")

</div>

In the end it wasn’t related to the loop but to the merge node not retriggering because not all inputs where reupdated: [Double merge node do not retrigger the workflow](http://community.n8n.io/t/double-merge-node-do-not-retrigger-the-workflow/244809)
