{"id":6200,"date":"2024-12-26T11:09:11","date_gmt":"2024-12-26T11:09:11","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=6200"},"modified":"2024-12-26T11:09:13","modified_gmt":"2024-12-26T11:09:13","slug":"how-to-store-objects-in-html5-local-storage-session-storage","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/","title":{"rendered":"How to Store Objects in HTML5 Local Storage\/Session Storage"},"content":{"rendered":"\n<p>Often, web developers need to store data pertaining to the present web page displayed on web browser. Typically, they rely on browser cookies for this purpose. But it has certain limitations such as size limit of 4kb. HTML 5 provides you a couple of simple ways to easily store and retrieve data on your web page &#8211; LocalStorage and SessionStorage APIs. You can not only store simple data such as numbers &amp; strings but also complex data types such as JavaScript objects in them. In this article, we will learn how to store objects in HTML5 Local Storage\/Session Storage.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#What_is_HTML5_Web_Storage\" >What is HTML5 Web Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Types_of_Web_Storage\" >Types of Web Storage<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Local_Storage\" >Local Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Session_Storage\" >Session Storage<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#How_to_Store_Objects_in_HTML5_Local_StorageSession_Storage\" >How to Store Objects in HTML5 Local Storage\/Session Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#How_to_Store_Objects_in_HTML5_Local_Storage\" >How to Store Objects in HTML5 Local Storage<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Add_Objects_to_Local_Storage\" >Add Objects to Local Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Check_Local_Storage\" >Check Local Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Remove_Local_Storage\" >Remove Local Storage<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#How_to_Store_Objects_in_Session_Storage\" >How to Store Objects in Session Storage<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Add_Session_Storage\" >Add Session Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Check_Session_Storage\" >Check Session Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Remove_Session_Storage\" >Remove Session Storage<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Local_Storage_vs_Session_Storage\" >Local Storage vs Session Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Check_Browser_Support_for_Web_Storage\" >Check Browser Support for Web Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#FAQ\" >FAQ<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_HTML5_Web_Storage\"><\/span>What is HTML5 Web Storage<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>These days websites and applications have become very data intensive and web developers need to often store data on client web browsers. Earlier people used <a href=\"https:\/\/en.wikipedia.org\/wiki\/HTTP_cookie\">HTML cookies<\/a> for this purpose. But cookies can store data only up to 4kb space. If you want to store larger data such as JSON response, table data, etc. you need to use Web storage API provided by web browsers.<\/p>\n\n\n\n<p>It is more secure than passing data over cookies, allows you to store large amounts of data (up to 5Mb) and offers better performance. These data are stored as key-value pairs making it easy to store, modify and access page data, as compared to cookie values. Both key as well as value should be strings. The web browser will maintain a separate list of key-value pairs for each origin-protocol combination.<\/p>\n\n\n\n<p>Web storage is organized per origin and protocol. So you can store data for an origin and protocol at one place, allowing all pages from that origin to access the data. It offers two types of storage &#8211; Local storage and Session storage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Types_of_Web_Storage\"><\/span>Types of Web Storage<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are two types of web storage supported by HTML5 web browsers &#8211; Local Storage and Session Storage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Local_Storage\"><\/span>Local Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Local storage allows you to store data locally on web browser, with no expiration date. It will continue to remain even when the web browser is closed and re-opened later. This local storage can be accessed on any window or tab. As mentioned earlier, browser will maintain different list of key-value pairs for each domain-protocol combination. Key-value pairs of one domain-protocol will not be accessible from another domain-protocol. When you open a website, you can only access local storage of that domain and not any other domain. The Local Storage allows you to save client data as key-value pair, modify as well as retrieve it using keys.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Session_Storage\"><\/span>Session Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Session storage is similar to local storage, except that it automatically gets deleted at the end of browser session. In other words, its data is accessible only as long as the browser tab remains open. Like Local Storage, web browser maintains a separate list of key-value pairs for each domain-protocol combination. Here too, key-value pairs of one domain-protocol will not be accessible from another domain-protocol. When you open a website, you can only access session storage of that domain and not any other domain. The Session Storage allows you to save client data as key-value pair, modify as well as retrieve it using keys.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Store_Objects_in_HTML5_Local_StorageSession_Storage\"><\/span>How to Store Objects in HTML5 Local Storage\/Session Storage<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now that we have a basic understanding of how web storage works, let us learn how to use them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Store_Objects_in_HTML5_Local_Storage\"><\/span>How to Store Objects in HTML5 Local Storage<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us learn how to add data to LocalStorage, how to retrieve and remove it. In all cases, we will refer to the global localStorage object that is available by default on all pages of web browser that supports Local Storage API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Add_Objects_to_Local_Storage\"><\/span>Add Objects to Local Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>There are two ways to add data to local storage &#8211; using setItem() function as well as using localStorage.key=value notation. We will look at both of them. Here is how to add two data &#8211; for company name and location.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localStorage.setItem(\"name\",\"ABC Company\");<br>localStorage.setItem(\"location\",\"New York\");<\/pre>\n\n\n\n<p>In the above code, we need to supply 2 arguments to setItem() function &#8211; name of the key and value of the key. In the above example, name is the key and &#8216;ABC Company&#8217; is the value.<\/p>\n\n\n\n<p>We can do the same thing also as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localStorage.name = 'ABC Company';<br>localStorage.location = 'New York';<\/pre>\n\n\n\n<p>The above code will create two key-value pairs for the domain-protocol where it is executed. We need to assign the value to localStorage.key variable.<\/p>\n\n\n\n<p>You can also use the above functions to modify existing key value pairs. Here is an example to modify the above values. If you use names of existing keys, it will overwrite their existing values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localStorage.setItem(\"name\",\"XYZ Company\");<br>localStorage.setItem(\"location\",\"NYC\");<br><br>localStorage.name = 'XYZ Company';<br>localStorage.location = 'NYC';<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_Local_Storage\"><\/span>Check Local Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>On the other hand, you can access or retrieve the value of a key in localStorage using getItem() function or using localStorage.key variable.<\/p>\n\n\n\n<p>Here is how to get the value using getItem() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localStorage.getItem('name');<\/pre>\n\n\n\n<p>The above code will return the value of the key name, if present. Else it will return undefined. You can directly use this value in other JS code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log(localStorage.getItem('name'));<\/pre>\n\n\n\n<p>You can also access these values using localStorage.key variable. Here is an example to get value of name key.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localStorage.name;<\/pre>\n\n\n\n<p>Here is how to use it other code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log(localStorage.name);<\/pre>\n\n\n\n<p>If you want to find out the total number of key value pairs stored in localStorage API of a given domain, use localStorage.length variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Remove_Local_Storage\"><\/span>Remove Local Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Lastly, you can remove one or more keys from localStorage object using removeItem() function. Here is an example to remove keys name and location.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localStorage.removeItem('name');<br>localStorage.removeItem('location');<\/pre>\n\n\n\n<p>The above code will remove keys one by one. Please note, if the specified key is not present in local storage, the web browser will silently continue with execution. It will not give any warning or error messages.<\/p>\n\n\n\n<p>If you want to remove all keys for a given domain-protocol combination, you can use localStorage.clear().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localStorage.clear();<\/pre>\n\n\n\n<p>Please note, if you try to reset the value of a key using setItem() function to &#8221; or null or undefined, the key-value pair will continue to exist, with null string. When you use removeItem(), the key-value pair itself is removed.<\/p>\n\n\n\n<p>Here is a sample code snippet that first checks if local storage is supported. Then it stores data in it, then it retrieves and displays the stored value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if(window.localStorage){ <br>        localStorage.setItem(\"name\",\"PQR company\");<br>        var name=localStorage.getItem(\"name\");<br>        alert(\"Company name is \" + name);<br>        localStorage.removeItem(\"name\");<br>    }<br>    else{<br>        alert(\"local storage not supported\")<br>    }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Store_Objects_in_Session_Storage\"><\/span>How to Store Objects in Session Storage<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Just as local storage, you can also store session-specific data in Session Storage. It will be automatically deleted at the end of session. It cannot be accessed from another browser tab. But it will still be accessible on page refresh or reload.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Add_Session_Storage\"><\/span>Add Session Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>There are two ways to store data in sessionStorage object &#8211; using setItem() function and using sessionStorage.key=value notation.<\/p>\n\n\n\n<p>Here is how to add company name and location in sessionStorage.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sessionStorage.setItem(\"name\",\"ABC Company\");<br>sessionStorage.setItem(\"location\",\"New York\");<\/pre>\n\n\n\n<p>Here also, we need to supply two arguments &#8211; key and its value. In the above code, name is the key and &#8216;ABC Company&#8217; is the value.<\/p>\n\n\n\n<p>You can also assign the values to keys as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sessionStorage.name = 'ABC Company';<br>sessionStorage.location = 'New York';<\/pre>\n\n\n\n<p>Both these code snippets will create 2 key-value pairs for the given session for the domain-protocol where they are executed.<\/p>\n\n\n\n<p>If these keys already exist, and you run these code, then their values will be overwritten. In other words, you can also use setItem() or sessionStorage.key to modify values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sessionStorage.setItem(\"name\",\"XYZ Company\");<br>sessionStorage.setItem(\"location\",\"NYC\");<br><br>sessionStorage.name = 'XYZ Company';<br>sessionStorage.location = 'NYC';<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_Session_Storage\"><\/span>Check Session Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Just as in the case of localStorage API, you can access the stored keys using getItem() function or as sessionStorage.key variable.<\/p>\n\n\n\n<p>Here is an example to retrieve key name&#8217;s value using getItem().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log(sessionStorage.getItem('name')); \/\/ output is ABC Company<\/pre>\n\n\n\n<p>If the specified key is not present in sessionStorage, it will return undefined value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log(sessionStorage.getItem('city')); \/\/ output is undefined<\/pre>\n\n\n\n<p>You can also directly access keys using sessionStorage.key.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log(sessionStorage.name); \/\/ output is ABC Company<\/pre>\n\n\n\n<p>If you want to find out how many key-value pairs are store in session storage, you can use its length property.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log(sessionStorage.length); \/\/ output is 2<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Remove_Session_Storage\"><\/span>Remove Session Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Lastly, you can remove any key value using removeItem(). <\/p>\n\n\n\n<p>Here is an example to remove keys name and location.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sessionStorage.removeItem('name');<br>sessionStorage.removeItem('location');<\/pre>\n\n\n\n<p>If the specified keys are not present in sessionStorage, then it will silently continue with execution. It will not throw any error or warnings.<\/p>\n\n\n\n<p>If you want to delete all key-value pairs for this domain-protocol&#8217;s session, then you can call clear() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localStorage.clear();<\/pre>\n\n\n\n<p>Here also, if you try to reset the value of a key-value pair using setItem() function to empty quotes or null or undefined, the key-value pair will continue to exist, with null string. But when you use removeItem() function, the key-value pair itself will be removed.<\/p>\n\n\n\n<p>Here is an example where we first check if session storage exists. If so, we add a data to it, then we retrieve this stored value, and finally remove it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if(window.sessionStorage){ <br>        sessionStorage.setItem(\"name\",\"PQR company\");<br>        var name=sessionStorage.getItem(\"name\");<br>        alert(\"Company name is \" + name);<br>        sessionStorage.removeItem(\"name\");<br>    }<br>    else{<br>        alert(\"session storage not supported\")<br>    }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Local_Storage_vs_Session_Storage\"><\/span>Local Storage vs Session Storage<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here is a quick summary of the key differences between local and session storage.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>Local Storage<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Session Storage<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Storage of up to 5Mb per domain<\/td><td class=\"has-text-align-center\" data-align=\"center\">Storage of up to 5Mb per domain<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">No expiry date for data<\/td><td class=\"has-text-align-center\" data-align=\"center\">Data expires at the end of session<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Can be accessed from other tabs<\/td><td class=\"has-text-align-center\" data-align=\"center\">Can be accessed on same tab only<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Supported by HTML 5 browsers only<\/td><td class=\"has-text-align-center\" data-align=\"center\">Supported by HTML5 browsers only<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_Browser_Support_for_Web_Storage\"><\/span>Check Browser Support for Web Storage<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Although web storage is supported by most modern web browsers such as Chrome, Edge, Firefox, Opera, etc. it is always good to check first before you store data. window.localStorage object will be non-null value in web browsers that support Local Storage. Else it will be undefined. Similarly, window.SessionStorage object will have non-null value if SessionStorage is supported. Else it will be false. Here is the code to check if Local Storage is present on your web browser.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if(window.localStorage)<br>{<br>    alert(\"Local storage supported\")<br>}<br>else<br>{<br>    alert(\"Local storage not supported\")<br>}<\/pre>\n\n\n\n<p>Here is the code to check if Session Storage is supported or not.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if(window.sessionStorage)<br>{<br>    alert(\"Session storage supported\")<br>}<br>else<br>{<br>    alert(\"Session storage not supported\")<br>}<\/pre>\n\n\n\n<p>If you want to check for both local as well as session storage with a single code, you can do it as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (typeof(Storage) !== \"undefined\") {<br>  alert('Web storage supported');<br>} else {<br>  alert('Web storage not supported');<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt how you can store objects in HTML5 local storage \/ session storage using HTML5 Web storage API. In it, we also learnt how Local Storage and Session Storage work. We learnt how to add data to local storage and session storage, how to retrieve these values and also how to remove them. You can use any of these storages, depending on your requirement. Please note, while using web storage, it is better to avoid using whitespace characters in key names and also avoid starting the key name with numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQ\"><\/span>FAQ<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. When to use Local Storage?<\/strong><\/p>\n\n\n\n<p>Local storage is useful if you want to retain data on client browser for long time, and make it accessible across multiple tabs<\/p>\n\n\n\n<p><strong>2. When to use Session Storage?<\/strong><\/p>\n\n\n\n<p>Session Storage is useful if you only want to store session-specific data that you want to retain till end of session and want to auto delete them when session ends. It is also useful if you want to retain data across page refresh or reload.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-remove-duplicate-values-from-js-array\/\">How to Remove Duplicate Values from JS Array<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-disable-browser-autocomplete-and-autofill\/\">How to Disable Browser Autocomplete and Autofill<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/\">Difference Between Let and Var in JavaScript<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often websites need to store web page data. Here is how to store objects in HTML5 Local and Session Storage.<\/p>\n","protected":false},"author":1,"featured_media":6233,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[346],"tags":[363,364,365],"class_list":["post-6200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-html","tag-html5-storage","tag-local-storage","tag-session-storage"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Store Objects in HTML5 Local Storage\/Session Storage - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often websites need to store web page data. Here is how to store objects in HTML5 Local and Session Storage.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Store Objects in HTML5 Local Storage\/Session Storage - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often websites need to store web page data. Here is how to store objects in HTML5 Local and Session Storage.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-26T11:09:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-26T11:09:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/how-to-store-data-in-local-session-storage-html5.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Store Objects in HTML5 Local Storage\\\/Session Storage\",\"datePublished\":\"2024-12-26T11:09:11+00:00\",\"dateModified\":\"2024-12-26T11:09:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/\"},\"wordCount\":1811,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1\",\"keywords\":[\"html5 storage\",\"local storage\",\"session storage\"],\"articleSection\":[\"HTML\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/\",\"name\":\"How to Store Objects in HTML5 Local Storage\\\/Session Storage - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1\",\"datePublished\":\"2024-12-26T11:09:11+00:00\",\"dateModified\":\"2024-12-26T11:09:13+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often websites need to store web page data. Here is how to store objects in HTML5 Local and Session Storage.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1\",\"width\":300,\"height\":200,\"caption\":\"store objects in html5 local \\\/ session storage\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-store-objects-in-html5-local-storage-session-storage\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Store Objects in HTML5 Local Storage\\\/Session Storage\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Store Objects in HTML5 Local Storage\/Session Storage - Ubiq BI","description":"Often websites need to store web page data. Here is how to store objects in HTML5 Local and Session Storage.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/","og_locale":"en_US","og_type":"article","og_title":"How to Store Objects in HTML5 Local Storage\/Session Storage - Ubiq BI","og_description":"Often websites need to store web page data. Here is how to store objects in HTML5 Local and Session Storage.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-12-26T11:09:11+00:00","article_modified_time":"2024-12-26T11:09:13+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/how-to-store-data-in-local-session-storage-html5.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Store Objects in HTML5 Local Storage\/Session Storage","datePublished":"2024-12-26T11:09:11+00:00","dateModified":"2024-12-26T11:09:13+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/"},"wordCount":1811,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1","keywords":["html5 storage","local storage","session storage"],"articleSection":["HTML"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/","name":"How to Store Objects in HTML5 Local Storage\/Session Storage - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1","datePublished":"2024-12-26T11:09:11+00:00","dateModified":"2024-12-26T11:09:13+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often websites need to store web page data. Here is how to store objects in HTML5 Local and Session Storage.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1","width":300,"height":200,"caption":"store objects in html5 local \/ session storage"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-store-objects-in-html5-local-storage-session-storage\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Store Objects in HTML5 Local Storage\/Session Storage"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/how-to-store-data-in-local-session-storage-html5.jpg?fit=300%2C200&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1C0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=6200"}],"version-history":[{"count":33,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6200\/revisions"}],"predecessor-version":[{"id":6235,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6200\/revisions\/6235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/6233"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=6200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=6200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=6200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}