Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Web Storage Api in JavaScript

Web Storage Api in JavaScript

The Web Storage API in JavaScript provides simple key–value storage in the browser. It comes in two main types:


? 1. localStorage

  • Stores data with no expiration.

  • Data persists even after the browser is closed.

? 2. sessionStorage

  • Stores data for one session.

  • Data is cleared when the tab/window is closed.


? Basic Usage Examples

? Set an item

localStorage.setItem('username', 'John');sessionStorage.setItem('theme', 'dark');

? Get an item

const user = localStorage.getItem('username'); // "John"

? Remove an item

localStorage.removeItem('username');

? Clear all storage

localStorage.clear();sessionStorage.clear();

? Example: Storing and Retrieving Data

// Save datalocalStorage.setItem("isLoggedIn", "true");// Read dataif (localStorage.getItem("isLoggedIn") === "true") {  console.log("User is logged in");}

? Storing Objects (use JSON)

const user = { name: "Alice", age: 30 };// StorelocalStorage.setItem("user", JSON.stringify(user));// Retrieveconst storedUser = JSON.parse(localStorage.getItem("user"));console.log(storedUser.name); // "Alice"

?? Notes

  • Storage is per origin (protocol + domain + port).

  • localStorage typically supports 5–10MB per origin.

  • Avoid storing sensitive data (it's not secure).

  • Works only in the browser, not in Node.js.


Let me know if you want a practical example (e.g., storing user preferences or cart data)!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql