# Cypress Documentation
# Source: https://raw.githubusercontent.com/cypress-io/cypress-documentation/main/docs/api/commands/its.mdx
# Path: docs/api/commands/its.mdx

---
title: 'its | Cypress Documentation'
description: Get a property's value on the previously yielded subject in Cypress.
sidebar_label: its
slug: /api/commands/its
---

<ProductHeading product="app" />

# its

Get a property's value on the previously yielded subject.

:::info

If you want to call a `function` on the previously yielded subject, use
[`.invoke()`](/api/commands/invoke).

:::

## Syntax

```javascript
.its(propertyName)
.its(propertyName, options)
```

### Usage

<Icon name="check-circle" color="green" /> **Correct Usage**

```javascript
cy.wrap({ width: '50' }).its('width') // Get the 'width' property
cy.window().its('sessionStorage') // Get the 'sessionStorage' property
```

<Icon name="exclamation-triangle" color="red" /> **Incorrect Usage**

```javascript
cy.its('window') // Errors, cannot be chained off 'cy'
cy.clearCookies().its('length') // Errors, 'clearCookies' does not yield Object
```

### Arguments

<Icon name="angle-right" /> **propertyName _(String, Number)_**

Index, name of property or name of nested properties (with dot notation) to get.

<Icon name="angle-right" /> **options _(Object)_**

Pass in an options object to change the default behavior of `.its()`.

| Option    | Default                                                           | Description                                                                         |
| --------- | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| `log`     | `true`                                                            | Displays the command in the [Command log](/app/core-concepts/open-mode#Command-Log) |
| `timeout` | [`defaultCommandTimeout`](/app/references/configuration#Timeouts) | Time to wait for `.its()` to resolve before [timing out](#Timeouts)                 |

<HeaderYields />

- `.its()` 'yields the value of the property.
- `.its()` is a query, and it is _safe_ to chain further commands.

## Examples

### Objects

#### Get property

```javascript
cy.wrap({ age: 52 }).its('age').should('eq', 52) // true
```

### Arrays

#### Get index

```javascript
cy.wrap(['Wai Yan', 'Yu']).its(1).should('eq', 'Yu') // true
```

### DOM Elements

#### Get the `length` property of a DOM element

```javascript
cy.get('ul li') // this yields us a jquery object
  .its('length') // calls 'length' property returning that value
  .should('be.gt', 2) // ensure the length is greater than 2
```

### Requests

#### Get the `user` object of the response's `body`

```javascript
cy
  .request(...)
  .its('body.user')
  .then(user => ...)
```

alternatively, use destructuring

```javascript
cy
  .request(...)
  .its('body')
  .then(({user}) => ...)
```

### Strings

#### Get `length` of title

```javascript
cy.title().its('length').should('eq', 24)
```

### Functions

#### Get function as property

```javascript
const fn = () => {
  return 42
}

cy.wrap({ getNum: fn }).its('getNum').should('be.a', 'function')
```

#### Access function properties

You can access functions to then drill into their own properties instead of
invoking them.

```javascript
// Your app code
// a basic Factory constructor
const Factory = (arg) => {
  // ...
}

Factory.create = (arg) => {
  return new Factory(arg)
}

// assign it to the window
window.Factory = Factory
```

```javascript
cy.window() // yields window object
  .its('Factory') // yields Factory function
  .invoke('create', 'arg') // now invoke properties on it
```

#### Use `.its()` to test `window.fetch`

:::info

[Check out our example recipe on testing `window.fetch` using `.its()`](/app/references/recipes#Stubbing-and-spying)

:::

### Nested Properties

You can drill into nested properties by using _dot notation_.

```javascript
const user = {
  contacts: {
    work: {
      name: 'Kamil',
    },
  },
}

cy.wrap(user).its('contacts.work.name').should('eq', 'Kamil') // true
```

### Existence

#### Wait for some property to exist on `window`

```javascript
cy.window()
  .its('globalProp')
  .then((globalProp) => {
    // do something now that window.globalProp exists
  })
```

#### Assert that a property does not exist on `window`

```javascript
cy.window().its('evilProp').should('not.exist')
```

## Rules

<HeaderRequirements />

- `.its()` requires being chained off a previous command.

<HeaderAssertions />

- `.its()` will automatically [retry](/app/core-concepts/retry-ability) until
  it has a property that is not `null` or `undefined`.

<HeaderTimeouts />

- `.its()` can time out waiting for the property to exist.
- `.its()` can time out waiting for assertions you've added to pass.

## Command Log

**_Get `responseBody` of aliased route_**

```javascript
cy.intercept('/comments', { fixture: 'comments.json' }).as('getComments')
cy.get('#fetch-comments').click()
cy.wait('@getComments')
  .its('response.body')
  .should(
    'deep.eq',
    JSON.stringify([
      { id: 1, comment: 'hi' },
      { id: 2, comment: 'there' },
    ])
  )
```

The commands above will display in the Command Log as:

<DocsImage
  src="/img/api/its/xhr-response-its-response-body-for-testing.png"
  alt="Command Log for its"
/>

When clicking on `its` within the command log, the console outputs the
following:

<DocsImage
  src="/img/api/its/response-body-yielded-with-its-command-log.png"
  alt="Console Log for its"
/>

## History

| Version                                  | Changes                                                       |
| ---------------------------------------- | ------------------------------------------------------------- |
| [3.8.0](/app/references/changelog#3-8-0) | Added support for `options` argument                          |
| [3.7.0](/app/references/changelog#3-7-0) | Added support for arguments of type Number for `propertyName` |

## See also

- [`.invoke()`](/api/commands/invoke)
- [`.then()`](/api/commands/then)
- [`cy.wrap()`](/api/commands/wrap)
- [Adding custom properties to the global `window` with the right TypeScript type](https://github.com/bahmutov/test-todomvc-using-app-actions#intellisense)