Local Storage is a browser-based storage system that allows websites to save key-value data on a user's device for persistent access.
There are two main types of web storage in modern browsers:
The Local Storage API provides the following methods:
setItem(key, value): Stores a key-value pair in Local Storage.getItem(key): Retrieves the value associated with the specified key.removeItem(key): Removes a specific key-value pair.clear(): Removes all key-value pairs from Local Storage.key(index): Returns the key at the specified index.length: Returns the number of key-value pairs stored.Note: Local Storage only stores strings. To store complex data like objects or arrays, you must
serialize them using JSON.stringify() and deserialize using JSON.parse().
Below are different methods to use Local Storage, as demonstrated in your provided code, along with explanations.
This method directly assigns a value to localStorage using a property name. It’s simple but not the
standard approach.
const nameElement = document.querySelector('.name-tag');
nameElement.innerText = localStorage.MyName;
console.log(nameElement);
Explanation: This retrieves MyName from Local Storage and sets it as the text of an
element. However, it lacks event handling and doesn’t update Local Storage dynamically.
This method uses setItem to store data and updates the UI in real-time as the user types.
const nameElement = document.querySelector('.name-tag');
const nameInput = document.querySelector('.name');
nameInput.addEventListener('input', (e) => {
localStorage.setItem('MyName', e.target.value);
nameElement.innerText = localStorage.getItem('MyName');
});
Explanation: The input event listener captures user input, stores it in Local
Storage using setItem, and updates the UI with getItem. This is better but still uses
a single key, limiting scalability.
This extends Method 2 to handle multiple inputs (name and age).
const nameElement = document.querySelector('.name-tag');
const ageElement = document.querySelector('.age-tag');
const nameInput = document.querySelector('.name');
const ageInput = document.querySelector('.age');
nameInput.addEventListener('input', (e) => {
localStorage.setItem('myName', e.target.value);
nameElement.innerText = localStorage.getItem('myName');
});
ageInput.addEventListener('input', (e) => {
localStorage.setItem('myAge', e.target.value);
ageElement.innerText = localStorage.getItem('myAge');
});
Explanation: This handles two inputs independently, storing and retrieving myName
and myAge. It’s more practical but still uses separate keys, which can become cumbersome for
complex data.
This method stores data as a JSON object, making it scalable for multiple properties.
const nameElement = document.querySelector('.name-tag');
const ageElement = document.querySelector('.age-tag');
const nameInput = document.querySelector('.name');
const ageInput = document.querySelector('.age');
const userData = JSON.parse(localStorage.getItem('userData')) || {};
nameInput.addEventListener('input', (e) => {
userData.name = e.target.value;
localStorage.setItem('userData', JSON.stringify(userData));
nameElement.innerText = e.target.value;
});
ageInput.addEventListener('input', (e) => {
userData.age = e.target.value;
localStorage.setItem('userData', JSON.stringify(userData));
ageElement.innerText = e.target.value;
});
if (userData.name) {
nameElement.innerText = userData.name;
}
if (userData.age) {
ageElement.innerText = userData.age;
}
Explanation: This retrieves a userData object from Local Storage (or creates an
empty one if none exists). It updates the object’s properties, serializes it to JSON, and stores it under a
single key. On page load, it checks for existing data to populate the UI. This is the most robust and scalable
approach.
Try entering your name and age below. The data will be stored in Local Storage and persist across page refreshes.
Advantages:
Limitations:
| Feature | Local Storage | Session Storage | Cookies |
|---|---|---|---|
| Storage Limit | 5–10 MB | 5–10 MB | 4 KB |
| Lifetime | Until cleared manually | Until tab is closed | Set by expiry or session |
| Scope | All tabs (same origin) | Current tab only | Client & server |
| Sent with HTTP? | No | No | Yes |
| Use Case | Preferences, theme | Form data, tab state | Login, tracking |
| Access via | localStorage |
sessionStorage |
document.cookie |