Web Storage
- Using Web Storage we can store data within web browser.
- Within Web Storage we can stored large amount of data locally.
- Data within Web Storage is secured.
Objects in Web Storage
- window.localStorage – can not expire data(even if we can removed it).
- window.sessionStorage – data lost when browser tab closed.
Methods in Web Storage
- setItem(name, value) – Create/add data in localStorage/sessionStorage. Contains name and value.
- getItem(name) – get/retrieve data from localStorage/sessionStorage.
- removeItem(name) – remove data from localStorage.
NOTE: removeItem() method is only applicable for localStorage to remove data. sessionStorage data lost when browser tab close.
Example
Store data in localStorage
1 2 3 |
localStorage.setItem("firstName","Anil"); //OR localStorage.firstName="Anil"; |
Get data form localStorage
1 2 3 |
localStorage.getItem("firstName"); //OR localStorage.firstName |
Remove item from localStorage
1 |
localStorage.removeItem("firstName"); |