{
  "doc": {
    "id": "api/commands/each",
    "title": "cy.each()",
    "description": "Iterate through an array like structure in Cypress (arrays or objects with a length property).",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/each.md",
    "version": "fbc9225067c51c52ee13224e3b702cf8a025ec12",
    "updated_at": "2026-08-14T12:36:26.878Z",
    "headings": [
      {
        "id": "api/commands/each#each",
        "text": "each",
        "level": 1
      },
      {
        "id": "api/commands/each#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/each#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/each#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/each#yields",
        "text": "Yields",
        "level": 3
      },
      {
        "id": "api/commands/each#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/each#dom-elements",
        "text": "DOM Elements",
        "level": 3
      },
      {
        "id": "api/commands/each#promises",
        "text": "Promises",
        "level": 3
      },
      {
        "id": "api/commands/each#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/each#return-early",
        "text": "Return early",
        "level": 3
      },
      {
        "id": "api/commands/each#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/each#requirements",
        "text": "Requirements",
        "level": 3
      },
      {
        "id": "api/commands/each#assertions",
        "text": "Assertions",
        "level": 3
      },
      {
        "id": "api/commands/each#timeouts",
        "text": "Timeouts",
        "level": 3
      },
      {
        "id": "api/commands/each#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/each#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/each#syntax",
      "doc_id": "api/commands/each",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\n.each(callbackFn)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.get('ul>li').each(() => {...}) // Iterate through each 'li'cy.getCookies().each(() => {...}) // Iterate through each cookie\n```\n\n**Incorrect Usage**\n\n```\ncy.each(() => {...})            // Errors, cannot be chained off 'cy'cy.clock().each(() => {...})    // Errors, 'clock' does not yield an array\n```\n\n### Arguments\n\n**callbackFn _(Function)_**\n\nPass a function that is invoked with the following arguments:\n\n*   `value` - the current item in the collection\n*   `index` - the 0-based index of the current item in the collection\n*   `collection` - the original collection being iterated over\n\n### Yields\n\n*   `.each()` yields the same subject it was given.\n*   Because `.each()` is not a [query](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried), it is unsafe to chain further commands that act or assert on DOM elements after `.each()`, as those elements may have re-rendered and gone stale during iteration. Reading the yielded subject in a [`.then()`](/llm/markdown/api/commands/then.md) callback (as in the example below) is safe.\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 207
    },
    {
      "id": "api/commands/each#usage",
      "doc_id": "api/commands/each",
      "heading": "Usage",
      "heading_level": 3,
      "content_markdown": "### Usage\n\n**Correct Usage**\n\n```\ncy.get('ul>li').each(() => {...}) // Iterate through each 'li'cy.getCookies().each(() => {...}) // Iterate through each cookie\n```\n\n**Incorrect Usage**\n\n```\ncy.each(() => {...})            // Errors, cannot be chained off 'cy'cy.clock().each(() => {...})    // Errors, 'clock' does not yield an array\n```\n",
      "section": "api",
      "anchors": [
        "usage"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 60
    },
    {
      "id": "api/commands/each#arguments",
      "doc_id": "api/commands/each",
      "heading": "Arguments",
      "heading_level": 3,
      "content_markdown": "### Arguments\n\n**callbackFn _(Function)_**\n\nPass a function that is invoked with the following arguments:\n\n*   `value` - the current item in the collection\n*   `index` - the 0-based index of the current item in the collection\n*   `collection` - the original collection being iterated over\n",
      "section": "api",
      "anchors": [
        "arguments"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 60
    },
    {
      "id": "api/commands/each#yields",
      "doc_id": "api/commands/each",
      "heading": "Yields",
      "heading_level": 3,
      "content_markdown": "### Yields\n\n*   `.each()` yields the same subject it was given.\n*   Because `.each()` is not a [query](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried), it is unsafe to chain further commands that act or assert on DOM elements after `.each()`, as those elements may have re-rendered and gone stale during iteration. Reading the yielded subject in a [`.then()`](/llm/markdown/api/commands/then.md) callback (as in the example below) is safe.\n",
      "section": "api",
      "anchors": [
        "yields"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 80
    },
    {
      "id": "api/commands/each#examples",
      "doc_id": "api/commands/each",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### DOM Elements\n\n**Iterate over an array of DOM elements**\n\n```\ncy.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  }})\n```\n\n**The original array is always yielded**\n\nNo matter what is returned in the callback function, `.each()` will always yield the original array.\n\n```\ncy.get('li')  .should('have.length', 3)  .each(($li, index, $lis) => {    return 'something else'  })  .then(($lis) => {    expect($lis).to.have.length(3) // true  })\n```\n\nHere the `.then()` callback only _reads_ the yielded array, which is safe. If instead you needed to act or assert on DOM elements that your application may have re-rendered during iteration, re-query them with a Cypress query rather than reusing the subject yielded by `.each()`.\n\n### Promises\n\n**Promises are awaited**\n\nIf your callback function returns a `Promise`, it will be awaited before iterating over the next element in the collection.\n\n```\ncy.wrap([1, 2, 3]).each((num, i, array) => {  return new Cypress.Promise((resolve) => {    setTimeout(() => {      resolve()    }, num * 100)  })})\n```\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 252
    },
    {
      "id": "api/commands/each#dom-elements",
      "doc_id": "api/commands/each",
      "heading": "DOM Elements",
      "heading_level": 3,
      "content_markdown": "### DOM Elements\n\n**Iterate over an array of DOM elements**\n\n```\ncy.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  }})\n```\n\n**The original array is always yielded**\n\nNo matter what is returned in the callback function, `.each()` will always yield the original array.\n\n```\ncy.get('li')  .should('have.length', 3)  .each(($li, index, $lis) => {    return 'something else'  })  .then(($lis) => {    expect($lis).to.have.length(3) // true  })\n```\n\nHere the `.then()` callback only _reads_ the yielded array, which is safe. If instead you needed to act or assert on DOM elements that your application may have re-rendered during iteration, re-query them with a Cypress query rather than reusing the subject yielded by `.each()`.\n",
      "section": "api",
      "anchors": [
        "dom-elements"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 185
    },
    {
      "id": "api/commands/each#promises",
      "doc_id": "api/commands/each",
      "heading": "Promises",
      "heading_level": 3,
      "content_markdown": "### Promises\n\n**Promises are awaited**\n\nIf your callback function returns a `Promise`, it will be awaited before iterating over the next element in the collection.\n\n```\ncy.wrap([1, 2, 3]).each((num, i, array) => {  return new Cypress.Promise((resolve) => {    setTimeout(() => {      resolve()    }, num * 100)  })})\n```\n",
      "section": "api",
      "anchors": [
        "promises"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 64
    },
    {
      "id": "api/commands/each#rules",
      "doc_id": "api/commands/each",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements\n\n*   `.each()` requires being chained off a previous command.\n\n### Assertions\n\n*   `.each()` will only run assertions you have chained once, and will not [retry](/llm/markdown/app/core-concepts/retry-ability.md).\n\n### Timeouts\n\n*   `.each()` can time out waiting for a promise you've returned to resolve.\n",
      "section": "api",
      "anchors": [
        "rules"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 59
    }
  ]
}