> For the complete documentation index, see [llms.txt](https://docs.datajet-app.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datajet-app.com/liquid/tags/pop.md).

# pop

Removes and returns the last element from an array. The original array is modified (mutated) and the removed element can be stored in a variable.

```liquid
{% json items %}[1, 2, 3, 4, 5]{% endjson %}
{% pop items as last_item %}

{% log last_item %}
{% comment %} Output: 5 {% endcomment %}

{% log items %}
{% comment %} Output: [1, 2, 3, 4] {% endcomment %}
```

#### Syntax

```liquid
{% pop array_name as variable_name %}
```

#### Parameters

| Parameter          | Required | Description                               |
| ------------------ | -------- | ----------------------------------------- |
| `array_name`       | Yes      | The array to remove the last element from |
| `as variable_name` | No       | Variable to store the removed element     |

#### Behavior

* Removes the **last** element from the array
* Modifies the original array (mutation)
* Returns `null` if the array is empty
* Supports nested array access with dot notation

#### Examples

**Basic pop operation:**

```liquid
{% json queue %}["task1", "task2", "task3"]{% endjson %}
{% pop queue as task %}

{% log "Processing: " | append: task %}
{% comment %} Output: Processing: task3 {% endcomment %}
```

**Pop without storing the value:**

```liquid
{% json numbers %}[1, 2, 3, 4, 5]{% endjson %}
{% pop numbers %}

{% log numbers %}
{% comment %} Output: [1, 2, 3, 4] {% endcomment %}
```

**Process array in reverse (LIFO):**

```liquid
{% json stack %}["first", "second", "third"]{% endjson %}

{% for i in (1..3) %}
  {% pop stack as item %}
  {% if item %}
    {% log "Popped: " | append: item %}
  {% endif %}
{% endfor %}

{% comment %}
Output:
Popped: third
Popped: second
Popped: first
{% endcomment %}
```

**Use with push for stack operations:**

```liquid
{% json undo_stack %}[]{% endjson %}

{% comment %} Add actions to stack {% endcomment %}
{% push undo_stack, "action1" %}
{% push undo_stack, "action2" %}
{% push undo_stack, "action3" %}

{% log "Stack: " %}
{% log undo_stack %}
{% comment %} Output: ["action1", "action2", "action3"] {% endcomment %}

{% comment %} Undo last action {% endcomment %}
{% pop undo_stack as last_action %}
{% log "Undoing: " | append: last_action %}
{% comment %} Output: Undoing: action3 {% endcomment %}
```

**Pop from nested array:**

```liquid
{% json data %}
  {
    "pending": ["order1", "order2", "order3"],
    "completed": []
  }
{% endjson %}

{% pop data.pending as order %}
{% log "Processing order: " | append: order %}
{% comment %} Output: Processing order: order3 {% endcomment %}
```

**Batch processing with pop:**

```liquid
{% json items_to_process %}
  [
    {"id": 1, "name": "Item A"},
    {"id": 2, "name": "Item B"},
    {"id": 3, "name": "Item C"}
  ]
{% endjson %}

{% assign processed_count = 0 %}

{% for i in (1..100) %}
  {% if items_to_process.size == 0 %}
    {% break %}
  {% endif %}

  {% pop items_to_process as item %}
  {% log "Processing: " | append: item.name %}

  {% comment %} Process item... {% endcomment %}

  {% assign processed_count = processed_count | plus: 1 %}
{% endfor %}

{% log "Total processed: " | append: processed_count %}
```

**Combine with conditionals:**

```liquid
{% json errors %}["Error 1", "Error 2"]{% endjson %}

{% if errors.size > 0 %}
  {% pop errors as latest_error %}
  {% log "Latest error: " | append: latest_error %}
{% else %}
  {% log "No errors" %}
{% endif %}
```

#### Related Tags

| Tag    | Description                            |
| ------ | -------------------------------------- |
| `push` | Adds an element to the end of an array |

#### Notes

* The array must exist and be a valid array, otherwise an error is thrown
* Pop modifies the original array - use with caution if you need to preserve the original
* Useful for implementing stack (LIFO - Last In, First Out) data structures
* When the array is empty, the popped value will be `null`/`undefined`
* Supports dot notation for nested arrays (e.g., `data.items`)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.datajet-app.com/liquid/tags/pop.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
