---
id: api/commands/each
title: each | Cypress Documentation
description: >-
  Iterate through an array like structure in Cypress (arrays or objects with a
  `length` property).
section: api
source_path: docs/api/commands/each.mdx
version: 3cf5b86b3403f604bdf7f3e35025c3bc3865e02c
updated_at: '2026-05-07T17:44:31.931Z'
---
# each

Iterate through an array like structure (arrays or objects with a `length` property).

It is [unsafe](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried) to chain further commands that rely on the subject after `.each()`.

## Syntax

```
.each(callbackFn)
```

### Usage

**Correct Usage**

```
cy.get('ul>li').each(() => {...}) // Iterate through each 'li'cy.getCookies().each(() => {...}) // Iterate through each cookie
```

**Incorrect Usage**

```
cy.each(() => {...})            // Errors, cannot be chained off 'cy'cy.clock().each(() => {...})    // Errors, 'clock' does not yield an array
```

### Arguments

**callbackFn _(Function)_**

Pass a function that is invoked with the following arguments:

*   `value`
*   `index`
*   `collection`

### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)

*   `.each()` yields the same subject it was given.
*   It is [unsafe](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried) to chain further commands that rely on the subject after `.each()`.

## Examples

### DOM Elements

**Iterate over an array of DOM elements**

```
cy.get('ul>li').each(($el, index, $list) => {  // $el is a wrapped jQuery element  if ($el.someMethod() === 'something') {    // wrap this element so we can    // use cypress commands on it    cy.wrap($el).click()  } else {    // do something else  }})
```

**The original array is always yielded**

No matter what is returned in the callback function, `.each()` will always yield the original array.

```
cy.get('li')  .should('have.length', 3)  .each(($li, index, $lis) => {    return 'something else'  })  .then(($lis) => {    expect($lis).to.have.length(3) // true  })
```

### Promises

**Promises are awaited**

If your callback function returns a `Promise`, it will be awaited before iterating over the next element in the collection.

```
cy.wrap([1, 2, 3]).each((num, i, array) => {  return new Cypress.Promise((resolve) => {    setTimeout(() => {      resolve()    }, num * 100)  })})
```

## Notes

### Return early

**Stop `each` prematurely**

You can stop the `.each()` loop early by returning `false` in the callback function.

## Rules

### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)

*   `.each()` requires being chained off a previous command.

### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions)

*   `.each()` will only run assertions you have chained once, and will not [retry](/llm/markdown/app/core-concepts/retry-ability.md).

### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts)

*   `.each()` can time out waiting for a promise you've returned to resolve.

## Command Log

*   `cy.each()` does _not_ log in the Command Log

## See also

*   [`.spread()`](/llm/markdown/api/commands/spread.md)
*   [`.then()`](/llm/markdown/api/commands/then.md)
