![]() |
VOOZH | about |
JavaScript localStorage is a feature that lets you store data in your browser using key-value pairs. The data stays saved even after you close the browser, so it can be used again when you open it later. This helps keep track of things like user preferences or state across different sessions.
ourStorage = window.localStorage;The above will return a storage object that can be used to access the current origin's local storage space.
localStorage has a simple API that allows you to interact with the browserβs local storage. The basic operations include storing, retrieving, updating, and removing items from the storage.
To store data in localStorage, use the setItem() method. This method accepts two arguments:
Syntax:
localStorage.setItem('key', 'value');To retrieve the data you stored, use the getItem() method. This method takes the key as an argument and returns the associated value. If the key does not exist, it returns null.
let value = localStorage.getItem('key');To remove a specific item from localStorage, use the removeItem() method. This method accepts the key of the item you want to remove.
Syntax:
localStorage.removeItem('key');If you want to clear all data stored in localStorage, use the clear() method. This will remove all key-value pairs stored in the localStorage for the current domain.
localStorage.clear();You can check if a key exists in localStorage by using getItem(). If the key doesnβt exist, getItem() will return null.
if (localStorage.getItem('username') !== null) {
console.log('Username exists in localStorage');
} else {
console.log('Username does not exist in localStorage');
}
This example demonstrates using localStorage to store, update, retrieve, and delete key/value pairs in the browser. It shows setting items, updating them, retrieving data by key, checking stored items count, and clearing the storage.
In this Example:
Output:
Now, follow the below given steps for performing the basic operations of localStorage:
localStorage only stores data as strings. If you need to store objects, arrays, or other complex data types, you must convert them into a string format. You can use JSON.stringify() to convert an object into a JSON string, and JSON.parse() to convert it back into an object.
Example: Storing and Retrieving an Object
In this example:
user is stored as a string using JSON.stringify().JSON.parse() is used to convert the string back into an object.| Method | Description |
|---|---|
setItem(key, value) | Stores a key/value pair |
getItem(key) | Returns the value in front of the key |
key(index) | Gets the key at a given index |
length | Returns the number of stored items (data) |
removeItem(key) | Removes the given key with its value |
clear() | Deletes everything from the storage |
window.localStorage is supported in the below given browsers:
| Browsers | π Chrome | π Edge | π Firefox | π Safari | π Opera |
|---|---|---|---|---|---|
| Support | Yes | Yes | Yes | Yes | Yes |
localStorage is an effective tool for storing data persistently within the browser. It provides methods to easily set, retrieve, and manage data, ensuring it remains available across sessions until explicitly cleared. By understanding and utilizing localStorage, developers can enhance the user experience by maintaining state and preferences across browsing sessions.