JavaScript provides three mechanisms for storing data on the client − cookies, session storage, and local storage. Each one has advantages and disadvantages. Show Local storage is the most recent mechanism. It allows for larger amounts of data to be stored, but the data is not deleted when the browser is closed. Local storage is useful for storing data that the user will need to access later, such as offline data. Session storage is similar to cookies, but the data is only stored for the current session. This means that the data will be deleted when the user closes the browser. Session storage is useful for storing data that is sensitive, such as login credentials. Cookies are the oldest and most well-known mechanism. They are simple to use and well supported by browsers. However, they are limited to 4KB of data and are often used to store data that is not sensitive, such as user preferences. In this tutorial, we are going to look at each of them in detail. Local StorageMost web applications these days require some type of user input, whether it be for a username, shipping address, or even just a preferences setting. This input is then usually sent off to a server somewhere to be processed and stored. However, what if your application needs to store data locally on the user’s computer? This is where Local Storage comes in. Local Storage is a type of web storage that allows JavaScript to store and access data right in the browser. This is especially useful for storing data that you want to persist even if the user closes the browser, such as preferences or settings. The data in Local Storage is stored in key/value pairs. The key is like the name of the data, and the value is like the actual data itself. You can think of it as a variable in JavaScript. To store data in Local Storage, you first need to create a key. Then you can store any data you want under that key. To create a key, you use the setItem() method. This method takes two arguments, the first is the key, and the second is the value you want to store. localStorage.setItem('key', 'value'); Now that you have a key, you can store any data you want under that key. The data you store can be any data type that JavaScript supports, including strings, numbers, objects, and arrays. To store data, you use the setItem() method again. This time, you pass in the key as the first argument and the data you want to store as the second argument. localStorage.setItem('key', 'value'); To retrieve data from Local Storage, you use the getItem() method. This method takes the key as an argument and returns the data that is stored under that key. localStorage.getItem('key'); If you want to remove an item from Local Storage, you use the removeItem() method. This method takes the key as an argument and removes the data that is stored under that key. localStorage.removeItem('key'); Session StorageSession Storage is a type of web storage that allows web applications to store data locally within the user's browser. Unlike cookies, data stored in session storage is specific to the site on which it was created and data is not shared with other sites. Session Storage is a new feature introduced in HTML5 that allows you to store data locally in the user's browser. Unlike cookies, data stored in session storage is specific to the site on which it was created and data is not shared with other sites. Session Storage is a way of storing data on the client side of an application. It's similar to local storage, but with a few key differences −
Session Storage is a great way to improve the performance of your web applications by reducing the amount of data that needs to be transferred between the client and server. It can also be used to store data in a more secure way since the data is not stored in cookies where it can be accessed by third-party sites. To use Session Storage in your web applications, you'll need to use the sessionStorage object. This object provides access to the current session's storage. The sessionStorage object has two methods setItem() − This method sets a key/value pair in the session storage. sessionStorage.setItem("name", "tutorialsPoint"); getItem() − This method retrieves the value of a key from the session storage. var name = sessionStorage.getItem("name"); The sessionStorage object also has a couple of other properties −
Session Storage is a great way to improve the performance of your web applications and store data in a more secure way. CookiesA cookie is a small piece of data that is stored on the user's computer. Cookies are used to store information about the user such as their name, password, and preferences. Cookies are created using the document.cookie property. This property is used to set, get, and delete cookies. Setting a CookieA cookie is set using the setItem() method. This method accepts two arguments, the name of the cookie and the value of the cookie. The name of the cookie is used to identify the cookie, and the value is the information that is to be stored in the cookie. The following code sets a cookie with the name "name" and the value "tutorialsPoint". document.cookie = "name=tutorialsPoint"; Reading a CookieA cookie is read using the getItem() method. This method accepts the name of the cookie as an argument and returns the value of the cookie. If the cookie does not exist, the getItem() method will return null. The following code reads the "name" cookie and stores the value in the "user" variable. var user = document.cookie.getItem("name"); One advantage of cookies over local Storage and session Storage is that they can be set to expire at a certain time, which makes them a good choice for storing data that should not be persisted for a long time, such as session IDs. Difference between Local Storage, Session Storage, and CookiesThe following table highlights the major differences between Local Storage, Session Storage, and Cookies −
ConclusionIn this tutorial, we discussed the differences between local storage, session storage, and cookies. We have seen different methods to store and retrieve data from this storage. Local storage is the most recent mechanism. It allows for larger amounts (10MB) of data to be stored, but the data is not deleted when the browser is closed. Session storage is similar to cookies, but the data is only stored for the current session. This means that the data will be deleted when the user closes the browser. Cookies are the oldest and most well-known mechanism. They are simple to use and well-supported by browsers. However, they are limited to 4KB of data and are often used to store data that is not sensitive, such as user preferences.
Updated on 29-Jul-2022 08:20:15
What is difference between localStorage and session storage?sessionStorage is similar to localStorage ; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the page session ends. Whenever a document is loaded in a particular tab in the browser, a unique page session gets created and assigned to that particular tab.
What is localStorage in JavaScript?The localStorage read-only property of the window interface allows you to access a Storage object for the Document 's origin; the stored data is saved across browser sessions.
What is local storage and session storage in react JS?localStorage is a web storage object that allows JavaScript sites and apps to keep key-value pairs in a web browser with no expiration date. This means the data survives page refreshes (sessionStorage) and even browser restarts.
What is a session storage?Session storage is a popular choice when it comes to storing data on a browser. It enables developers to save and retrieve different values. Unlike local storage, session storage only keeps data for a particular session. The data is cleared once the user closes the browser window.
|