Stores data permanently in the browser (until manually cleared).
Key Features:
Code:
// Save data
localStorage.setItem("theme", "dark");
// Get data
const theme = localStorage.getItem("theme");
// Remove
localStorage.removeItem("theme");
Stores data only for the current tab session only for the current tab session.
Code:
// Save data
sessionStorage.setItem("step", "2");
// Get data
const step = sessionStorage.getItem("step");
// Clear
sessionStorage.clear();
Cookies are small pieces of data stored in the browser and automatically sent to the server with every HTTP request.
Code:
// Set cookie document.cookie = "username=Abhishek; expires=Fri, 31 Dec 2026 12:00:00 UTC; path=/"; // Read cookies console.log(document.cookie); // Delete cookie document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";