What other ways of storing data in the browser do you know besides cookies?
sobes.tech AI
Answer from AI
In addition to cookies, there are several ways to store data in a browser:
-
LocalStorage — stores data as key-value pairs, accessible only on the client side, data persists indefinitely until manually deleted.
-
SessionStorage — similar to LocalStorage, but data only lives within a single browser session (deleted when the tab is closed).
-
IndexedDB — a more complex and powerful database in the browser, allows storing structured data, supports transactions and queries.
-
Cache Storage (Cache API) — used for storing request responses, often used in PWA for offline access.
-
Web SQL (deprecated) — an outdated API for storing data in SQL format, not recommended for use.
Example of using LocalStorage:
// Write data
localStorage.setItem('username', 'Ivan');
// Read data
const user = localStorage.getItem('username');
// Delete data
localStorage.removeItem('username');
These methods allow storing data on the client side without needing to send it to the server with each request.