> For the complete documentation index, see [llms.txt](https://stash-it.gitbook.io/stash-it/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stash-it.gitbook.io/stash-it/api/cacheinstance/hasitem-key.md).

# hasItem(key)

Checks if item exists.

### **Arguments**

1. `key` *(string)*: Key to check if an item exists by.

### **Lifecycle**

1. **preHasItem**

   **Event name:** `preHasItem`\
   **Properties passed:**

   * `cacheInstance` reference to cache instance (`this`)
   * `key` key passed to `hasItem` method

   **Returns:** *(object)*: object containing properties:

   * `cacheInstance` reference to cache instance (`this`)
   * `key` key passed through handlers added for `preHasItem` event<br>
2. **postHasItem**

   **Event name:** `postHasItem`\
   **Properties passed:**

   * `cacheInstance` reference to cache instance (`this`) returned by `preHasItem`
   * `key` key returned by `preHasItem`
   * `result` boolean value returned by adapter using its [hasItem](/stash-it/api/adapter/hasitem-key.md) method

   **Returns:** *(object)*: object containing properties:

   * `cacheInstance` reference to cache instance (`this`)
   * `key` key passed through handlers added for `postHasItem` event
   * `result` boolean value passed through handlers added for `postHasItem` event<br>

   Eventually `hasItem` returns result returned by `postHasItem`.

{% hint style="info" %}
Even though `hasItem` uses `buildKey` internally, that build key is not passed in `postHasItem` event handler's object's properties. If key is somehow changed (with some plugin) during `preBuildKey` or `postGetKey` event handlers, that key is not passed further in `hasItem`. If you need access to built key, you can still obtain it from returned item to which you have access in `postHasItem`.
{% endhint %}

### **Returns**

*(bool)*: `true` if item exists, `false` otherwise.

### **Example**

```javascript
// assuming that you already have cache instance prepared
cache.hasItem('key'); // true
cache.hasItem('keyForItemThatDoesNotExist'); // false
```
