JavaScript localStorage for Beginners: Save Data Easily
Learn how to use JavaScript localStorage step by step. Save, read and delete data in the browser with simple, copy-paste examples made for absolute beginners.
Learn how to use JavaScript localStorage step by step. Save, read and delete data in the browser with simple, copy-paste examples made for absolute beginners.
How do you save data in JavaScript so it is still there after the page reloads? The simple answer is localStorage. It is a small storage box built into every web browser. You put a value in with setItem, take it out with getItem, and the browser keeps it, even after you close the tab or restart the computer.
In this beginner guide you will learn what localStorage is, how to save, read and delete data, how to store arrays and objects, and which mistakes to avoid. You do not need any earlier experience beyond basic variables and functions.
localStorage is a feature of the browser that lets a website save small pieces of text on the visitor's own device. Each piece is saved as a key and a value. The key is a name (like "theme"). The value is the data (like "dark"). Think of it as a labelled drawer: the label is the key, and what is inside is the value.
Normal JavaScript variables disappear when the page reloads. localStorage data stays. That makes it perfect for things like a dark mode choice, a user's name, or a simple to-do list.
Use localStorage.setItem(key, value) to save something. Open your browser, press F12, go to the Console tab, and try this.
localStorage.setItem("username", "Sam");
localStorage.setItem("theme", "dark");Line 1 saves the value "Sam" under the key "username". Line 2 saves "dark" under the key "theme". Both keys and values are written inside quotes because they are text (called strings). If you save to a key that already exists, the old value is replaced.
Use localStorage.getItem(key) to read a value back.
const name = localStorage.getItem("username");
console.log(name); // Sam
const missing = localStorage.getItem("age");
console.log(missing); // nullThe first line reads the value stored under "username" and puts it in a variable called name. The console then prints Sam. The third line asks for a key that does not exist. In that case getItem gives back null, which means "nothing here". Always be ready for null when you read data.
localStorage.removeItem("theme");
localStorage.clear();removeItem deletes just one key, here "theme". clear deletes everything your website has saved in localStorage. Use clear carefully, because there is no undo.
| Method | What it does | Example |
|---|---|---|
| setItem(key, value) | Saves or updates a value | localStorage.setItem("theme", "dark") |
| getItem(key) | Reads a value (or null) | localStorage.getItem("theme") |
| removeItem(key) | Deletes one key | localStorage.removeItem("theme") |
| clear() | Deletes all keys | localStorage.clear() |
Let us build something useful. This code saves the visitor's theme choice and applies it again the next time they open the page. If you are new to changing the page with JavaScript, read our DOM manipulation guide first.
const button = document.querySelector("#toggle");
const saved = localStorage.getItem("theme");
if (saved === "dark") {
document.body.classList.add("dark");
}
button.addEventListener("click", () => {
document.body.classList.toggle("dark");
const isDark = document.body.classList.contains("dark");
localStorage.setItem("theme", isDark ? "dark" : "light");
});Line 1 finds the button on the page. Line 3 reads the saved theme. Lines 4 to 6 say: if the saved theme is dark, add the dark class to the page right away. Line 8 listens for clicks on the button. Inside, line 9 switches the dark class on or off, line 10 checks whether it is now on, and line 11 saves "dark" or "light" so the choice is remembered. Reload the page and your choice stays.
New to changing page elements with JavaScript? Read our DOM manipulation guide for beginners before building more examples.
Read the DOM guidelocalStorage can only store strings. If you try to save an array or object directly, JavaScript turns it into useless text.
localStorage.setItem("fruits", ["apple", "banana"]);
console.log(localStorage.getItem("fruits")); // apple,bananaThe result is the plain text "apple,banana", not a real array. You cannot loop over it or use array methods. To fix this, convert the data to a JSON string when saving and convert it back when reading. JSON is just a text format for data.
const todos = ["Buy milk", "Learn JavaScript"];
localStorage.setItem("todos", JSON.stringify(todos));
const text = localStorage.getItem("todos");
const list = JSON.parse(text);
console.log(list[1]); // Learn JavaScriptLine 1 creates an array. Line 2 uses JSON.stringify to turn the array into the string '["Buy milk","Learn JavaScript"]' and saves it. Line 4 reads the string. Line 5 uses JSON.parse to turn it back into a real array. Line 6 prints the second item. The same two steps work for objects.
const todos = JSON.parse(localStorage.getItem("todos")) || [];
todos.push("Write a blog post");
localStorage.setItem("todos", JSON.stringify(todos));On the first visit, nothing is saved, so getItem returns null and JSON.parse(null) gives null. The || [] part means "or else use an empty array". Then we add a new item with push and save the updated list again. This read, change, save pattern is how most small apps use localStorage.
| Feature | localStorage | sessionStorage | Cookies |
|---|---|---|---|
| Lasts until | You delete it | The tab is closed | An expiry date |
| Size limit | About 5 MB | About 5 MB | About 4 KB |
| Sent to server | No | No | Yes, on every request |
| Best for | Preferences, small app data | Temporary tab data | Login sessions |
sessionStorage works exactly like localStorage, with the same methods, but its data disappears when the tab closes. Cookies are tiny and are sent to the server with each request, so they suit things like logins.
localStorage is a built-in browser feature that saves key and value pairs on the visitor's device. The data stays after the page reloads or the browser closes. It is used for small things like settings and simple app data.
It lasts until you or the user deletes it. It has no expiry date. A user can clear it from the browser settings, and private browsing windows usually erase it when closed.
Convert it to a string with JSON.stringify before saving it with setItem. When you read it with getItem, turn it back using JSON.parse. This keeps its structure intact.
Most browsers allow about 5 MB per website. That is plenty for settings and small lists, but too small for large files or big databases.
No. Data in localStorage is not encrypted and can be read by any script running on your page. Keep passwords and other sensitive data on the server instead.
You now know how to save, read and delete data in the browser. Try the practice ideas above, then combine localStorage with event listeners and arrays to build small apps that remember what users do.
Want help turning your idea into a real web app? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.