# setLocalStorageItem

## `setLocalStorageItem` Function

#### Description

With this method, a value can be stored in the **localStorage** of the browser. A **TTL (Time-To-Live)** can be set to define how long the item should persist before being considered expired. If no TTL is set, the configured system default is used.

This function helps ensure client-side storage respects the JENTIS storage policy.

***

#### Parameters

| Name               | Type                    | Description                                                                                                                    |
| ------------------ | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `key`              | `String`                | The key under which the value is stored in localStorage                                                                        |
| `value`            | `Any`                   | The value to be stored (will be stringified internally)                                                                        |
| `ttl` *(optional)* | `Number` (milliseconds) | The duration the item should remain valid. If not set or too high, it will default to the configured `maximumStorageLifetime`. |

***

#### Returns

| Type      | Description                                                                                   |
| --------- | --------------------------------------------------------------------------------------------- |
| `Boolean` | Returns `true` if the item was successfully set, or `false` if localStorage is not available. |

***

#### Usage Example

```javascript
this.setLocalStorageItem("my_test_key", "my_value", 3600000);
```

Stores the value `"my_value"` under the key `"my_test_key"` for one hour (3600000 milliseconds).\
If the TTL exceeds the JENTIS configured `maximumStorageLifetime`, it will be automatically reduced to that maximum.

***

#### Important Notes

* The stored item will be saved in the following format:

  ```json
  {
    "value": "my_value",
    "ttl": 3600000,
    "expiry": 1652953200000
  }
  ```
* The method checks whether localStorage is available before attempting to write.
* If you define nested functions inside your codeblock, be aware that the `this` scope will not be available inside them.


---

# 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://docs.jentis.com/developer-guide/variables/client-side-variables/public-function-scope/setlocalstorageitem.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.
