Skip to content

Local Storage vs Session Storage vs Cookie

In web development, there are several ways to store data on the client-side, and three of the most common options are local storage, session storage, and cookies. Each of these options has its unique features and use cases.

Local Storage

Local storage is a client-side storage mechanism that allows web applications to store key-value pairs in the user's browser. The data stored in local storage persists even after the browser is closed, making it an excellent choice for storing data that needs to be accessed repeatedly. Local storage has a relatively large storage capacity compared to cookies, which can only store up to 4KB of data. Local storage can store up to 5-10 MB, depending on the browser.

Advantages:

Can store a larger amount of data than cookies. Data is persistent and does not expire, making it suitable for long-term storage. The data stored in local storage is available on all pages of the same domain.

Disadvantages:

Local storage is not suitable for storing sensitive information, as it is accessible to all scripts on the same domain. The data stored in local storage is not accessible across different domains. If a user clears their browser data, the stored data in local storage will also be deleted.

Session Storage

Session storage is another client-side storage mechanism that stores data temporarily and is cleared when the user closes the browser tab. Session storage has a similar storage capacity to local storage and is suitable for storing data that needs to be accessed within the same session. Session storage data is only accessible within the same tab or window where it was created.

Advantages:

Session storage is suitable for storing temporary data that does not need to persist across browser sessions. The data stored in session storage is only accessible within the same tab or window where it was created. Session storage is cleared automatically when the user closes the browser tab, making it a good option for storing sensitive information.

Disadvantages:

The data stored in session storage is not accessible across different tabs or windows. Session storage is not suitable for storing data that needs to persist across browser sessions.

Cookies

Cookies are small text files that are stored on the user's computer by a website. Cookies are commonly used to store user preferences, login information, and other session-specific data. Cookies have a relatively small storage capacity of up to 4KB and can be set to expire after a specified period.

Advantages:

Cookies are widely supported by all browsers and are suitable for storing small amounts of data. Cookies can be used to store login credentials and other sensitive information securely. Cookies can be set to expire after a specified period, making them suitable for storing temporary data.

Disadvantages:

Cookies have a small storage capacity and are not suitable for storing large amounts of data. Cookies are vulnerable to attacks like cross-site scripting (XSS) and cross-site request forgery (CSRF). Cookies can be disabled by the user, making them unreliable for storing critical information.

In summary, local storage, session storage, and cookies are three different client-side storage mechanisms, each with its unique features and use cases. By understanding the differences between these storage mechanisms, you can choose the best option for your specific needs.


Local StorageSession StorageCookies
Allows 10MB of data to be storedAllows 5MB of data to be storedStorage capacity is limited to 4KB of data
Stored data is not deleted when browser is closedData is stored only for the session and will be deleted when the browser is closedData can be set to expire at a certain time
Useful for storing data that you want to persist even if the user closes the browser, such as preferences or settingsUseful for storing data that is sensitive, such as login credentialsOften used to store data that is not sensitive, such as user preferences and session IDs.
Data is not sent with the request from the client to the serverData is not sent with the request from the client to the serverData is sent with the request from the client to the server
Data is stored on the browser and systemData is stored on the browser onlyData is stored on the browser only
Profile picture

I have a passion for all things web.