DEV Community

Cover image for Understanding Local Storage and Session Storage in JavaScript (Beginner’s Guide)
WISDOMUDO
WISDOMUDO

Posted on

Understanding Local Storage and Session Storage in JavaScript (Beginner’s Guide)

Learn the difference between Local Storage and Session Storage in JavaScript. In this beginner-friendly article, you’ll discover how to store and retrieve data in the browser through straightforward examples.

Introduction

When you build a website, you often need to store little bits of information, such as a user’s theme choice, login info, or cart items, right in their browser. This is where Local Storage and Session Storage come in.
Both are built into the Web Storage API, helping developers keep data on a user’s device for faster access. They make websites more rapid, more interactive, and user-friendly.

In this beginner’s guide, we’ll explain what Local Storage and Session Storage are, how they work, and when to use each one using simple, easy-to-follow examples.

What You’ll Learn

Once you complete this guide, you’ll fully understand:

  • What Local Storage and Session Storage are
  • How they differ from each other
  • How to store, retrieve, and delete data using both
  • When to use each in real-world applications

What Is Web Storage?

Before diving into Local and Session Storage, it’s important to understand the concept of Web Storage.

Web Storage allows websites to store data inside a user’s browser. Unlike cookies, which are sent back and forth with every request, Web Storage data stays in the browser, making it faster, safer, and more efficient.

Now that you know what Web Storage is, let’s explore its two main types: Local Storage and Session Storage.

What Is Local Storage?

Local Storage stores data in the browser with no expiration time. This means even if the user closes their browser or restarts their computer, the data will still be there.

It’s useful for storing things like dark mode preferences, saved login info, or user settings.

Example:

// Save data
localStorage.setItem("username", "Wisdom");

// Retrieve data
let user = localStorage.getItem("username");
console.log(user); // Output: Wisdom

// Remove data
localStorage.removeItem("username");

Enter fullscreen mode Exit fullscreen mode

Local Storage saves data as key-value pairs, where both the key and the value are stored as strings.

Now, let’s move to its temporary version, Session Storage.

What Is Session Storage?

Session Storage works much like Local Storage, but has one key difference: the data only lasts for as long as the browser tab is open.

It’s perfect for storing temporary information, like a user’s form progress or search results that must continue after the tab has been closed.

Example:

// Save data
sessionStorage.setItem("cartItem", "Laptop");

// Retrieve data
let item = sessionStorage.getItem("cartItem");
console.log(item); // Output: Laptop

// Remove data
sessionStorage.removeItem("cartItem");

Enter fullscreen mode Exit fullscreen mode

Once the user closes the tab, the stored data is automatically deleted.

Now, let’s compare the two side by side.

Local Storage vs Session Storage: Key Differences

Feature Local Storage Session Storage
Lifetime Persists until manually deleted Cleared when the tab is closed
Scope Shared across all tabs and windows Works in one tab only
Storage Limit Around 5–10 MB Around 5 MB
Use Case Saving user preferences or login info Temporary form or session data

When to Use Each

Use Local Storage when you need data to stay even after a user closes the browser, like saving app themes or authentication tokens.

Use Session Storage when data should only last for one visit, like keeping a user’s form data active while they’re filling it out.

Both tools help improve performance and user experience by reducing server requests and loading times.

Frequently Asked Questions (FAQ)

1. What is the main difference between Local Storage and Session Storage?

The key difference lies in how long the data remains. Local Storage saves data permanently, while Session Storage clears it when the tab is closed.

2. Can I store objects or arrays in Local Storage?

Yes, but you need to convert them to strings first using JSON.stringify().

Example:

localStorage.setItem("user", JSON.stringify({ name: "Wisdom", age: 30 }));

Enter fullscreen mode Exit fullscreen mode

To get it back, use JSON.parse() to turn it back into an object:

let user = JSON.parse(localStorage.getItem("user"));

Enter fullscreen mode Exit fullscreen mode

3. Is Local Storage secure for storing user passwords or sensitive data?

No. Never store passwords, tokens, or private data in Local Storage or Session Storage because anyone with access to the browser can read them. For sensitive data, use secure server-side storage and encryption.

4. How much data can I store in Local Storage or Session Storage?

Most browsers allow around 5 to 10 MB of storage per domain. This is much more than cookies, but it’s still meant for small data, not large files or media.

5. Do all modern browsers support Local Storage and Session Storage?

Yes, they are supported by all modern browsers, including Chrome, Firefox, Safari, and Edge. However, always check compatibility if you’re targeting ancient browsers.

Conclusion

Understanding Local Storage and Session Storage is an essential step for every JavaScript beginner. These browser-based storage tools improve app performance by storing lightweight data locally.

By knowing when to use each, you can build more efficient, user-friendly web experiences that feel smooth and responsive.

Whether it’s saving preferences or managing session data, mastering these two tools is a solid step forward in your JavaScript learning journey.

You can reach out to me via LinkedIn

Top comments (2)

Collapse
 
linjunjie525 profile image
Lin JunJie

Clear and beginner-friendly explanation!
Great examples that make Local vs Session Storage easy to understand.

Collapse
 
wisdomudo profile image
WISDOMUDO

I'm glad you find it useful. Thanks for engaging!