Featured Article

LocalStorage JavaScript Example: Save Data Like a Pro

Adam Jul 13, 2026

LocalStorage in JavaScript is a powerful tool for web developers, allowing data to be stored directly in the browser. Unlike cookies, which expire after a certain period, localStorage doesn't have a built-in expiration time. Here, we'll explore localStorage with practical examples.

JavaScript Local Storage: Store Data That Lasts πŸ’ΎπŸš€
JavaScript Local Storage: Store Data That Lasts πŸ’ΎπŸš€

Before we dive into the examples, let's understand the basic syntax. LocalStorage uses the window.localStorage object with methods like setItem(key, value), getItem(key), and removeItem(key).

Local Storage
Local Storage

Setting and Getting Values

To set a value, use setItem(key, value). The key is a string that uniquely identifies the value. The value can be any valid JavaScript data type, which is automatically stringified and stored.

{ LocalStorage JavaScript }
{ LocalStorage JavaScript }

Here's an example of setting and getting a value:

```javascript // Set a value window.localStorage.setItem('userName', 'JohnDoe'); // Get the value let username = window.localStorage.getItem('userName'); console.log(username); // Outputs: JohnDoe ```

Storing Objects

JavaScript Session Storage: Temporary Data Storage Made Easy πŸ’ΎπŸš€
JavaScript Session Storage: Temporary Data Storage Made Easy πŸ’ΎπŸš€

You can store complex data structures like objects. When you retrieve them, they're automatically parsed back into JavaScript objects.

Let's store a user object:

```javascript let user = { name: 'JohnDoe', age: 30 }; window.localStorage.setItem('user', JSON.stringify(user)); // To retrieve let storedUser = JSON.parse(window.localStorage.getItem('user')); console.log(storedUser); // Outputs: { name: 'JohnDoe', age: 30 } ```

Clearing LocalStorage

The Difference Between Local and Session Storage
The Difference Between Local and Session Storage

To remove all data, use the clear() method. You can also remove individual items with removeItem(key).

Example of clearing all data:

```javascript window.localStorage.clear(); ```

Handling LocalStorage Events

Cookies vs Local Storage in JavaScript
Cookies vs Local Storage in JavaScript

You can listen to storage events to perform actions when data is stored or removed. The storage event applies to all windows that share the same document domain.

Adding Event Listeners

To Do App With Local Storage | Step By Step Javascript Project
To Do App With Local Storage | Step By Step Javascript Project
Local storage vs Session storage in Frontend Development
Local storage vs Session storage in Frontend Development
Web Storage
Web Storage
What is localStorage used for?
What is localStorage used for?
an orange and white logo with the words local storage on it's left side
an orange and white logo with the words local storage on it's left side
LocalStorage in JavaScript: Save Data in Browser
LocalStorage in JavaScript: Save Data in Browser
How To Store Data In Javascript - Part 2 of 2
How To Store Data In Javascript - Part 2 of 2
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Objects: The Key to Structured Data πŸš€
JavaScript Objects: The Key to Structured Data πŸš€
the different types of computers are shown in this diagram, which shows how to use them
the different types of computers are shown in this diagram, which shows how to use them
JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘
Back to Basics - JavaScript Closures
Back to Basics - JavaScript Closures
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Roadmap + Projects
JavaScript Roadmap + Projects
Javascript Fundamental
Javascript Fundamental
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
JavaScript Return Values: Get the Most Out of Your Functions πŸš€
JavaScript Return Values: Get the Most Out of Your Functions πŸš€
Empty object check in Javascript
Empty object check in Javascript
Filter JavaScript Arrays Like a BOSS: Beginner's Guide
Filter JavaScript Arrays Like a BOSS: Beginner's Guide
Here’s What You Have To Know About JavaScript Closuers
Here’s What You Have To Know About JavaScript Closuers

Add an event listener with the addEventListener() method on the window object. The event object provides details about what changed.

Example of listening to storage events:

```javascript window.addEventListener('storage', (event) => { console.log('Key:', event.key); console.log('Old value:', event.oldValue); console.log('New value:', event.newValue); }); ```

LocalStorage is a valuable tool for persisting user data in the browser. It can be used to remember user preferences, remember user's last visited page, or for anything that doesn't need to be saved on the server.