Loading Please Wait...
The Web Storage API is a simple syntax for storing and retrieving data in the browser. It is very easy to use.
JavaScript provides two type of storage API.
Both localStorage and sessionStorage have the following methods and properties:
Property/Method | Description |
---|---|
key(n) | Returns the name of the nth key in the storage |
length | Returns the number of data items stored in the Storage object |
getItem(keyname) | Returns the value of the specified key name |
setItem(keyname) | Adds a key to the storage, or updates a key value (if it already exists) |
removeItem(keyname) | Removes that key from the storage |
clear() | Empty all key out of the storage |
The localStorage object provides access to a local storage for a particular Web Site. It allows you to store, read, add, modify, and delete data items for that domain.
The data is stored with no expiration date, and will not be deleted when the browser is closed.
The data will be available for days, weeks, and years unless the cache is cleared.
The sessionStorage object is identical to the localStorage object.
The difference is that the sessionStorage object stores data for one session.
The data is deleted when the browser is closed.
The setItem() method stores a data item in a storage.
It takes a key name and a value as parameters.
localStorage.setItem("company", "Lynxsia IT Solutions");
// OR
sessionStorage.setItem("company", "Lynxsia IT Solutions");
The getItem() method retrieves a data item from the storage.
It takes a key name as parameter.
localStorage.getItem("company");
// OR
sessionStorage.getItem("company");
The key() method returns name of the key with the specified index.
alert(localStorage.key(0));
// OR
alert(sessionStorage.key(0));
The removeItem() method removes the specified Storage Object item.
localStorage.removeItem("company");
// OR
sessionStorage.removeItem("company");
The clear() method removes all the Storage Object item.
localStorage.clear();
// OR
sessionStorage.clear();
The length property returns the number of items stored in the browsers Storage Object.
alert(localStorage.length);
// OR
alert(sessionStorage.length);
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved