DEV Community

Emin Vergil
Emin Vergil

Posted on • Updated on

What is the difference between session storage and local storage ?

Local storage and session storage are two important tools for web developers that allow them to store data in a user's web browser. While they may seem similar at first glance, there are some key differences between the two that are important to understand.

First, let's define what each type of storage is and how it is used. Local storage is a type of storage that persists across multiple sessions, meaning that the data stored in local storage is not deleted when the user closes the browser or shuts down their computer. This makes it useful for storing data that needs to be accessed again in the future, such as preferences or user settings.

On the other hand, session storage is only stored for a single session, and is deleted when the user closes the browser or ends their session. This makes it useful for storing temporary data that only needs to be accessed during a single session, such as the contents of a shopping cart in an online store.

One key difference between local storage and session storage is the amount of data that can be stored. Local storage allows for a larger amount of data to be stored, while session storage has a more limited capacity. This is because local storage is intended for long-term storage, while session storage is only meant for temporary use.

Another key difference is the way that data is accessed. Local storage is accessed using the localStorage object in JavaScript, while session storage is accessed using the sessionStorage object. This means that the two types of storage are isolated from each other, so data stored in local storage cannot be accessed from session storage, and vice versa.

In summary, local storage and session storage are two different types of storage that are available to web developers in modern browsers. Local storage is used for long-term storage of data that persists across multiple sessions, while session storage is used for temporary storage of data that is deleted when the user ends their session. Understanding the differences between the two can help web developers choose the right tool for their specific needs.

// Store data in local storage
localStorage.setItem("key", "value");

// Retrieve data from local storage
const localData = localStorage.getItem("key");

// Store data in session storage
sessionStorage.setItem("key", "value");

// Retrieve data from session storage
const sessionData = sessionStorage.getItem("key");

Enter fullscreen mode Exit fullscreen mode

This code shows how to use the setItem and getItem methods of the localStorage and sessionStorage objects to store and retrieve data in local and session storage, respectively. Note that the setItem and getItem methods take two arguments: a key (which is used to identify the data) and a value (which is the actual data to be stored).

In this example, we are storing a simple string value in both local storage and session storage. However, these methods can be used to store any type of data that can be serialized as a string, including arrays and objects.

It is also important to note that local and session storage are isolated from each other, so data stored in local storage cannot be accessed from session storage, and vice versa. This means that developers need to be careful to use the correct type of storage for their specific needs.

https://eminvergil.netlify.app/blog/session-localstorage

Top comments (0)