# hasItem(key)

### **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
```


---

# Agent Instructions: 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:

```
GET https://stash-it.gitbook.io/stash-it/api/cacheinstance/hasitem-key.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
