The Web Storage API offers a straightforward syntax for storing and retrieving data in the browser, making it easy to use.
localStorage.setItem(“name”, “John Doe”); localStorage.getItem(“name”); |
The Web Storage API is compatible with all modern browsers.
The localStorage
object provides access to local storage for a specific website, allowing you to store, read, add, modify, and delete data for that domain.
The data has no expiration date and is not deleted when the browser is closed.
It can persist for days, weeks, or even years.
The localStorage.setItem() method stores a data item in the storage, accepting a name and a value as parameters.
localStorage.setItem(“name”, “John Doe”); |
The localStorage.getItem() method retrieves a data item from storage, taking the item’s name as a parameter.
localStorage.getItem(“name”); |
The sessionStorage object is similar to the localStorage object, but it stores data for a single session.
The data is deleted when the browser is closed.
sessionStorage.getItem(“name”); |
The sessionStorage.setItem() method stores a data item in session storage, accepting a name and a value as parameters.
sessionStorage.setItem(“name”, “John Doe”); |
The sessionStorage.getItem() method retrieves a data item from session storage, taking the item’s name as a parameter.
sessionStorage.getItem(“name”); |
Property/Method |
Description |
key(n) |
Returns the name of the nth key in the storage. |
length |
Returns the total number of data items stored in the Storage object. |
getItem(keyname) |
Returns the value associated with the specified key name. |
setItem(keyname, value) |
Adds a key to the storage or updates its value if the key already exists. |
removeItem(keyname) |
Removes the specified key from the storage. |
clear() |
Clears all keys from the storage. |
Property |
Description |
window.localStorage |
Allows saving key/value pairs in a web browser, with the data stored indefinitely and no expiration date. |
window.sessionStorage |
Allows saving key/value pairs in a web browser, with the data stored for the duration of a single session. |