Localstorage
Local Storage
Description
localStorage is a web storage object in JavaScript that allows websites to store key-value pairs persistently in a user's browser. Unlike sessionStorage, which clears data when the browser tab or window is closed, localStorage data remains available across browser sessions until explicitly removed by the user or the website.
setLocalStorageItem(key, value)
- 🧱 Purpose: Stores a value in localStorage under the specified key.
- 📌 Parameters:
key(string): The name of the storage entry.value(object | string): The value to store; objects are automatically stringified.
- ⚙️ Behavior:
- Converts objects to JSON strings before saving.
- Catches and logs any storage errors (e.g., quota exceeded).
getLocalStorageItem<T>(key)
- 🧱 Purpose: Retrieves and parses a value from
localStorage. - 📌 Parameters:
key (String): The key of the item to retrieve.
- ⚙️ Behavior:
- Automatically parses JSON strings into objects.
- Gracefully handles errors, logging them and returning null.
removeLocalStorageItem(key)
- 🧱 Purpose: Deletes an entry from localStorage using the given key.
- 📌 Parameters:
key (String): The key of the item to remove.
- ⚙️ Behavior:
- Silently logs errors if removal fails (e.g., access issues in restricted environments).
hasLocalStorageKey(key)
- 🧱 Purpose: Checks whether a specific key exists in localStorage.
- 📌 Parameters:
key (String): The key to check for.
- ⚙️ Returns:
boolean:trueif the key exists, otherwisefalse.