HTML5 – Web storage
Web storage is a way for web applications to store data locally in the user’s browser. HTML5 introduced two types of web storage: local storage
and session storage
.
Local storage
is a way to store data persistently in the user’s browser. The data stored in local storage remains available even after the user closes the browser or restarts the device.
Session storage
is a way to store data temporarily in the user’s browser. The data stored in session storage is deleted when the user closes the browser or the tab.
To use web storage, you can use the localStorage
and sessionStorage
objects, which are properties of the window
object. The localStorage
and sessionStorage
objects have a number of methods that you can use to store, retrieve, and delete data.
Here is an example of how to use the localStorage
object to store and retrieve data:
// Store data in local storage
localStorage.setItem('key', 'value');
// Retrieve data from local storage
var data = localStorage.getItem('key');
console.log(data); // Outputs 'value'
In this example, the setItem
method is used to store data in local storage, and the getItem
method is used to retrieve the data from local storage.
Here is an example of how to use the sessionStorage
object to store and retrieve data:
// Store data in session storage
sessionStorage.setItem('key', 'value');
// Retrieve data from session storage
var data = sessionStorage.getItem('key');
console.log(data); // Outputs 'value'
In this example, the setItem
method is used to store data in session storage, and the getItem
method is used to retrieve the data from session storage.