Javatpoint Logo
Javatpoint Logo

DIFFERENCE BETWEEN LOCAL STORAGE, SESSION, and COOKIES

One of the most crucial protocols for effective communication between the server and the client is the HTTP protocol. The fundamental drawback of the HTTP protocol is that it is a stateless protocol, meaning that it does not keep track of any information regarding requests and responses from either the server or the client. Therefore, there are three ways to track important information to solve this problem. This article will explain the distinctions between local storage, session storage, and cookies as well as the significance of each for web developers.

Local Storage:

Access to the Document's local storage object is provided by this read-only interface attribute; the data is saved across browser sessions. Similar to sessionStorage, with the exception that sessionStorage data is deleted when the page's session expires, or when the browser window is closed. When a browser's final "private" tab is closed, it is cleared (localStorage data for a document loaded in a private browsing or incognito session).

DOMStrings are storage formats that use the two-byte UTF-16 encoding standard to encode data. Similar to how they are for objects, strings are automatically created from integer keys. A protocol in the document is specific to the data kept in LocalStorage. LocalStorage delivers a different object when a website is loaded using HTTP than when it is loaded using HTTPS (such as https://abc.com).

The prerequisites for behaviour are unknown and may differ between various browsers if a document is loaded from a file: URL (that is, directly from the user's local file system instead of being loaded from the server). In all of the current browsers, localStorage seems to return a separate object for each file: URL. In essence, it appears that every URL: file has its own particular local storage space.

Because of the aforementioned ambiguous file: this behaviour cannot be guaranteed URL requirements. As a result, it's possible that browsers could ever change how they handle files. Some browsers' handling of it has changed over time.

4 techniques exist for local storage:

Method setItem() -

Two parameters are required by this method: a key and a value. It is used to store the value along with the name of the key in a specific location.

localStorage.setItem(key, value)

Method: getItem

This method just requires the key parameter, which is used to retrieve the value associated with a certain key name.

localStorage.getItem(key)

Method removeItem() -

The value that was stored in the memory with reference to the key can be removed using this method.

localStorage.removeItem(key)

Method clear() -

The values kept in local storage can all be cleared using this procedure.

localStorage.clear()

Session Storage: What is it?

The read-only sessionStorage property provides access to Session Storage objects. Local storage data never expires, in contrast to session storage data that is deleted after a page session ends, which is the difference between the two types of storage.

Once a document has loaded on a browser tab, a distinct page session has been generated. There can only be one open tab at a time for page sessions. After the page reloads and restores, the pages are only preserved for the duration that the tab or browser is open. In contrast to session cookies, a new session is started each time a tab or window is opened. The same URL opens many tabs or windows, each of which produces its own sessionStorage. The sessionStorage from the original tab is copied to the replicated tab when you duplicate a tab. A window or tab being closed ends the session and deletes all sessionStorage objects.

What information is kept in sessionStorage depends on the protocol of a page. Particularly, information stored by scripts that are viewed over HTTP (for example, http://abc.com) is stored in a different object from the same website when accessed via HTTPS (for example, https://abc.com). In UTF-16 DOMString format, a DOMString number has a character size of two bytes. Similar to how they are for objects, strings are automatically created from integer keys.

There are 4 ways to store sessions:

Method setItem() -

Two parameters are required by this method: a key and a value. It is used to store the value along with the name of the key in a specific location.

sessionStorage.setItem(key, value)

Method getIteam() -

This method just requires the key parameter, which is used to retrieve the value associated with a certain key name.

sessionStorage.getItem(key)

Method removeItem() -

The value that was stored in the memory with reference to the key can be removed using this method.

sessionStorage.removeItem(key)

Method clear() -

This technique is used to remove all values from the session storage.

sessionStorage.clear()

Cookie:

The word "cookie" only applies to textual data about a website. This website retains certain information in your local system when you visit a specific website to recognise you and provide you results based on your preferences. The use of cookies has long been a part of internet history. When someone visits a website, they request a web page from the server. Each request made to a server is distinct. Similarly, if you visit a hundred times, the server will recognise each visit as a separate request. It doesn't seem reasonable and clear to store every user's information on a server given the volume of requests it receives per second. If you don't come back, the same information might not be required again.

In order to uniquely identify you, a cookie is delivered and kept on your local computer. The next time you contact the server, it will respond since it has recognised you. Nearly all servers make use of this cookie (some exceptions exist today because of advertisements). As a result, even if you may have a lot of cookies on your computer, a server will still identify and examine them.

In the beginning, cookies were created to enhance the developer experience. Think about browsing a website in a language different than your own (say English). You can choose English as your language from the website's language menu. If you visit the same website five times a day, it might be necessary to switch languages five times. Therefore, a cookie is placed on your computer and contains these facts. It makes sure that the next time you send a request, the server will be aware that you want to access the website in English. In this context, cookies are important. Today's scale cookies are substantially smaller than the one in the previous sentence.

Differences between cookies, session storage, and local storage

local Storage Session Cookies
Local storage has a 5MB or 10MB storage capacity. The session storage has a 5MB storage capacity. Cookies have a storage capacity of 4 KB.
It can only be eliminated manually or using JavaScript because it is not session-based. It functions per window or tab and is session-based. Data is only kept in this manner for the length of a session, or until the browser (or tab) is closed. According to the settings and operations for each tab and window, cookies expire.
Only local storage can be read by the client. Only session storage can be read by the client. The cookies can be read and written by both clients and servers.
No information is sent to the server. No information is sent to the server. There is a data transfer to the server.
Fewer older browsers still support it. Fewer older browsers still support it. All browsers, including older ones, support it.
It is very helpful for storing data like preferences or settings that you want to persist even after the user quits the browser. Sensitive data, including login passwords, can be stored using session storage. Cookies are frequently used to store non-sensitive data, such user preferences.
HTML5 has a brand-new feature called Local Storage. HTML5 has a brand-new feature called "Session Storage". Cookies are the most well-known and oldest (HTML4-based) method.

Conclusion:

The distinctions between local storage, session storage, and cookies were covered in this tutorial. Various techniques for storing and obtaining data from this storage have been seen. The most recent method is local storage. Larger (10MB) data storage is possible. However, the data is not erased when the browser is closed. Similar to cookies, session storage only saves information for the current session. According to this, the data will be erased when the user exits the browser. The oldest and best-known mechanism is cookies. They are easy to use and have strong browser support. They can only hold 4KB of information, though, and are frequently used to store non-sensitive information like user preferences.


Next TopicDifference between





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA