Local Storage is a Web Storage API that allows you to store data in the browser as key-value pairs. The stored data remains available even after the browser is closed and reopened, until it is manually removed.
📌 What is Local Storage?
Local Storage is built into modern browsers and is accessed through thelocalStorage object. It is commonly used to store user preferences, application settings, shopping cart data, and other client-side information.
💡 Why Use Local Storage?
- 🌙 Save dark/light theme preference
- 👤 Remember user settings
- 📝 Store draft form data
- 🛒 Save shopping cart items
- ⚡ Improve user experience by reducing repeated input
🛠 Local Storage Methods
| Method | Description |
|---|---|
| setItem() | Stores a value. |
| getItem() | Retrieves a value. |
| removeItem() | Removes a specific item. |
| clear() | Removes all stored items. |
| key() | Returns the key at a given index. |
| length | Returns the number of stored items. |
📝 Storing Data
Use setItem() to save data.
Store a Value
localStorage.setItem("username", "John");📖 Reading Data
Retrieve data using getItem().
Read a Value
const username = localStorage.getItem("username");
console.log(username);✏️ Updating Data
Updating is the same as storing—use the same key with a new value.
Update a Value
localStorage.setItem("username", "Alice");🗑️ Removing Data
Remove a specific key using removeItem().
Remove a Value
localStorage.removeItem("username");🧹 Clearing All Data
Remove every item stored in Local Storage.
Clear Local Storage
localStorage.clear();📊 Checking the Number of Items
Length Property
console.log(localStorage.length);🔑 Accessing Keys
Retrieve the key name stored at a specific index.
Get Key
console.log(localStorage.key(0));📦 Storing Objects
Local Storage only stores strings. Convert objects to JSON before storing.
Store an Object
const user = {
name: "John",
age: 25,
city: "New York"
};
localStorage.setItem("user", JSON.stringify(user));📥 Retrieving Objects
Convert the stored JSON string back into an object.
Read an Object
const user = JSON.parse(localStorage.getItem("user"));
console.log(user.name);
console.log(user.age);📋 Storing Arrays
Store an Array
const fruits = ["Apple", "Banana", "Orange"];
localStorage.setItem("fruits", JSON.stringify(fruits));📤 Reading Arrays
Retrieve an Array
const fruits = JSON.parse(localStorage.getItem("fruits"));
console.log(fruits);🛠 Helper Functions
Save Data
saveData()
function saveData(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}Load Data
loadData()
function loadData(key) {
const data = localStorage.getItem(key);
return data ? JSON.parse(data) : null;
}Delete Data
deleteData()
function deleteData(key) {
localStorage.removeItem(key);
}🚀 Complete Example
User Settings Example
const settings = {
theme: "dark",
language: "English",
fontSize: 16
};
// Save
localStorage.setItem("settings", JSON.stringify(settings));
// Read
const savedSettings =
JSON.parse(localStorage.getItem("settings"));
console.log(savedSettings);
// Delete
localStorage.removeItem("settings");📊 Local Storage vs Session Storage vs Cookies
| Feature | Local Storage | Session Storage | Cookies |
|---|---|---|---|
| Storage Limit | ~5–10 MB | ~5 MB | ~4 KB |
| Expires | Never (until removed) | Tab closes | Configurable |
| Sent to Server | ❌ No | ❌ No | ✅ Yes |
| Accessible by JavaScript | ✅ Yes | ✅ Yes | Usually Yes |
⚠️ Limitations
- 📦 Only string values can be stored.
- 🌐 Data is limited to the same origin (protocol, domain, and port).
- 🚫 Not suitable for sensitive information.
- 📱 Storage limits vary slightly between browsers.
Note
✅ Best Practices
- 💾 Store only necessary data.
- 🔐 Never store passwords, authentication tokens, or confidential information.
- 📄 Use meaningful key names such as "userProfile" or "theme".
- 🧹 Remove outdated data to keep storage clean.
- ⚡ Handle cases where a key does not exist by checking for null.
🎯 Summary
Local Storage is a simple and powerful browser storage mechanism for saving persistent client-side data. It stores information as string-based key-value pairs, making it ideal for user preferences, application settings, and cached data. For objects and arrays, always useJSON.stringify() when saving andJSON.parse() when retrieving.