Last modified: Jul 20, 2026

JavaScript Local Storage Guide

JavaScript Local Storage is a simple way to save data in the user’s browser. It stores information as key-value pairs. This data stays even after the browser is closed. It is a part of the Web Storage API. Local Storage is easy to use and very helpful for web apps.

In this guide, you will learn how to use Local Storage. We will cover setting, getting, and removing data. We will also talk about storing objects and arrays. This article is for beginners. It uses short sentences and clear examples. You will be able to use Local Storage in your projects after reading this.

What is Local Storage?

Local Storage is a built-in browser feature. It lets you store data on the user’s computer. The data has no expiration time. It remains until you delete it manually. Each website gets its own Local Storage space. This means data from one site cannot be seen by another site.

Local Storage can store up to 5-10 MB of data. This is much more than cookies. It is also faster because the data stays on the client side. You do not need to send data to the server every time. This makes your web app faster and more responsive.

How to Use Local Storage

You use Local Storage with the localStorage object. This object is available in all modern browsers. It has four main methods: setItem(), getItem(), removeItem(), and clear(). You can also use the key() method to get a key by its index.

Let us start with the basics. The setItem() method adds a key-value pair. The getItem() method reads a value by its key. The removeItem() method deletes a specific key. The clear() method removes all data. Here is a simple example.


// Set a value
localStorage.setItem('username', 'Alice');

// Get a value
let user = localStorage.getItem('username');
console.log(user); // Output: Alice

// Remove a single item
localStorage.removeItem('username');

// Clear all data
localStorage.clear();

Alice

Storing Strings, Numbers, and Booleans

Local Storage only stores strings. When you use setItem(), the value is automatically converted to a string. This works fine for strings. But numbers and booleans become strings too. You need to convert them back when reading.

Here is an example with numbers and booleans.


// Store a number
localStorage.setItem('age', 25);
let age = localStorage.getItem('age');
console.log(age); // Output: "25" (string)
console.log(typeof age); // Output: string

// Convert back to number
let realAge = Number(age);
console.log(realAge); // Output: 25 (number)

// Store a boolean
localStorage.setItem('isLogged', true);
let logged = localStorage.getItem('isLogged');
console.log(logged); // Output: "true" (string)

// Convert back to boolean
let realLogged = logged === 'true';
console.log(realLogged); // Output: true (boolean)

25
string
25
true
true

Storing Objects and Arrays

Objects and arrays are not strings. You must convert them to strings first. Use JSON.stringify() to store them. Use JSON.parse() to read them back. This is a common pattern in Local Storage.

Let us store a user object and an array of items.


// Store an object
let user = { name: 'Bob', age: 30, city: 'New York' };
localStorage.setItem('user', JSON.stringify(user));

// Read the object back
let storedUser = JSON.parse(localStorage.getItem('user'));
console.log(storedUser.name); // Output: Bob

// Store an array
let colors = ['red', 'green', 'blue'];
localStorage.setItem('colors', JSON.stringify(colors));

// Read the array back
let storedColors = JSON.parse(localStorage.getItem('colors'));
console.log(storedColors[0]); // Output: red

Bob
red

Updating and Deleting Data

To update a value, use setItem() again with the same key. This overwrites the old value. To delete a specific key, use removeItem(). To delete everything, use clear(). These operations are fast and simple.

Here is an example of updating and deleting.


// Set initial value
localStorage.setItem('theme', 'light');

// Update the value
localStorage.setItem('theme', 'dark');
let theme = localStorage.getItem('theme');
console.log(theme); // Output: dark

// Delete the key
localStorage.removeItem('theme');
console.log(localStorage.getItem('theme')); // Output: null

// Clear all data
localStorage.clear();

dark
null

Checking if a Key Exists

You can check if a key exists in Local Storage. Use getItem() and test if the result is null. A null value means the key does not exist. You can also use the length property to see how many items are stored.


// Check if key exists
if (localStorage.getItem('username') !== null) {
    console.log('Key exists');
} else {
    console.log('Key does not exist');
}

// Get the number of items
console.log(localStorage.length); // Output: number of items

Key does not exist
0

Iterating Over Local Storage

You can loop through all keys in Local Storage. Use the key() method with an index. This is useful for debugging or exporting data. You can also use a for...in loop, but key() is more reliable.


// Set some data
localStorage.setItem('a', '1');
localStorage.setItem('b', '2');
localStorage.setItem('c', '3');

// Loop through all keys
for (let i = 0; i < localStorage.length; i++) {
    let key = localStorage.key(i);
    let value = localStorage.getItem(key);
    console.log(key + ': ' + value);
}

a: 1
b: 2
c: 3

Common Use Cases

Local Storage is great for many tasks. You can save user preferences like theme or language. You can store form data so users do not lose their work. You can cache API responses to reduce server load. You can also save game scores or progress.

One important use is remembering login state. You can store a token or a flag. But be careful with sensitive data. Local Storage is not encrypted. Never store passwords or credit card numbers here. For security, use server-side sessions or secure cookies.

Limitations and Best Practices

Local Storage is synchronous. This means it blocks the main thread. Do not store large amounts of data. It can slow down your page. Keep your data small and simple.

Local Storage is per origin. Each domain has its own storage. Subdomains have separate storage too. You cannot share data between different domains.

Always use try-catch when storing data. Some browsers may throw an error if storage is full. This is rare but good practice. Here is an example.


try {
    localStorage.setItem('data', 'some value');
} catch (e) {
    console.error('Storage is full or disabled');
}

Also, remember to clear unused data. This keeps your storage clean. Use removeItem() for old keys. Do not rely on users clearing their cache.

Related Concepts

Understanding Local Storage helps with other JavaScript topics. For example, JavaScript Variable Scope Explained shows how variables behave in different contexts. This is useful when managing state across functions.

Another related topic is Types of JavaScript Variables. Knowing the difference between var, let, and const helps you write cleaner code. This is important when you use Local Storage to save dynamic data.

Finally, JavaScript Variable Declaration Guide explains how to declare variables properly. This is a fundamental skill for any JavaScript developer.

Conclusion

JavaScript Local Storage is a powerful tool. It lets you save data in the browser easily. You learned how to use setItem(), getItem(), removeItem(), and clear(). You also learned how to store objects and arrays with JSON. Remember to convert data types when needed.

Use Local Storage for small, non-sensitive data. It is perfect for user preferences and simple caches. Always handle errors and clean up old data. With this guide, you can now add persistence to your web apps. Start using Local Storage today and make your applications smarter.