HTML Web Storage

HTML Web Storage, also known as DOM Storage, is a web browser feature that allows you to store data locally within the user's browser. It provides a way to store key-value pairs, similar to cookies, but with a larger storage capacity and simpler programming interface.

Web Storage consists of two mechanisms: `localStorage` and `sessionStorage`. Both mechanisms store data on the client-side but have some differences in terms of persistence and scope.


1. **localStorage**:

- The `localStorage` object allows you to store data that persists even after the browser is closed and reopened.
- The stored data has no expiration date and remains available until explicitly removed by the user or cleared programmatically.
- The data stored in `localStorage` is scoped to the entire domain.
- You can access `localStorage` using the `localStorage` object in JavaScript.


2. **sessionStorage**:

- The `sessionStorage` object allows you to store data that is specific to a particular browsing session.
- The stored data is available as long as the browser tab or window is open and is lost when the tab or window is closed.
- Like `localStorage`, the data in `sessionStorage` is also scoped to the entire domain.
- You can access `sessionStorage` using the `sessionStorage` object in JavaScript.


Here's an example of how to use `localStorage` in JavaScript:

javascript
// Storing data in localStorage
localStorage.setItem('key', 'value');

// Retrieving data from localStorage
var data = localStorage.getItem('key');

// Removing data from localStorage
localStorage.removeItem('key');

// Clearing all data from localStorage
localStorage.clear();

And here's an example of how to use `sessionStorage`:

javascript
// Storing data in sessionStorage
sessionStorage.setItem('key', 'value');

// Retrieving data from sessionStorage
var data = sessionStorage.getItem('key');

// Removing data from sessionStorage
sessionStorage.removeItem('key');

// Clearing all data from sessionStorage
sessionStorage.clear();

Both `localStorage` and `sessionStorage` provide the same API for storing, retrieving, removing, and clearing data. The main difference lies in the persistence and scope of the stored data.

It's important to note that web storage is subject to the same-origin policy, which means that data stored in one domain's localStorage/sessionStorage is not accessible to other domains.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext