# 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.
