{"id":1195,"date":"2023-07-17T05:00:00","date_gmt":"2023-07-17T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1195"},"modified":"2023-07-14T15:55:57","modified_gmt":"2023-07-14T22:55:57","slug":"localstorage-and-sessionstorage","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/localstorage-and-sessionstorage\/","title":{"rendered":"LocalStorage and SessionStorage"},"content":{"rendered":"\n<p>Last week, we covered the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/lifecycle-of-angular-applications\/\" target=\"_blank\" rel=\"noopener\" title=\"\">lifecycle of Angular applications<\/a> and <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/using-services-to-cache-data\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how services can be used as a cache<\/a> for our data.<\/p>\n\n\n\n<p>I mentioned that Angular apps are independent applications running in a browser tab and that closing such a tab &#8220;kills&#8221; the application and frees the memory used by our app &#8211; thus losing any data stored in our services. Using the browser&#8217;s refresh button would also clear everything &#8211; similar to a reboot of the application.<\/p>\n\n\n\n<p>If we want a more persistent cache, we can use the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/localStorage\" target=\"_blank\" rel=\"noopener\" title=\"\">localStorage<\/a> API from Javascript. Learning that API takes five seconds:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"58\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-10.png\" alt=\"\" class=\"wp-image-1196\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-10.png 450w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-10-300x39.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above code stores the string &#8220;Tom&#8221; in the browser <code>localStorage<\/code> under the key &#8220;myCat&#8221;. To read that value, later on, all we need is the following:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"339\" height=\"57\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-11.png\" alt=\"\" class=\"wp-image-1197\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-11.png 339w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-11-300x50.png 300w\" sizes=\"auto, (max-width: 339px) 100vw, 339px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And that&#8217;s it! Such storage will remain in place if the user closes your app&#8217;s tab or even closes the browser and shuts down their computer. Your data stays in the browser storage if the user does not clear the browser&#8217;s cache. <\/p>\n\n\n\n<p>Also, such data is stored based on the domain on your web app, so other websites cannot read your storage values, and you can&#8217;t access values from other websites either.<\/p>\n\n\n\n<p>One last tip: To store objects or arrays in <code>localStorage<\/code>, we have to turn them into strings, which means we usually do the following:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"100\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-12-1024x100.png\" alt=\"\" class=\"wp-image-1199\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-12-1024x100.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-12-300x29.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-12-768x75.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-12-1536x151.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-12.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And to read that object from <code>localStorage<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"100\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-13-1024x100.png\" alt=\"\" class=\"wp-image-1200\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-13-1024x100.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-13-300x29.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-13-768x75.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-13-1536x151.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-13.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Note that <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/sessionStorage\" title=\"\">sessionStorage<\/a><\/code> has the same API as <code>localStorage<\/code>, but follows different rules and has a shorter life span: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A page session lasts as long as the tab or the browser is open and survives over page reloads and restores.<\/li>\n\n\n\n<li>Opening a page in a new tab or window creates a new session with the value of the top-level browsing context.<\/li>\n\n\n\n<li>Opening multiple tabs\/windows with the same URL creates <code>sessionStorage<\/code> for each tab\/window.<\/li>\n\n\n\n<li>Duplicating a tab copies the tab&#8217;s <code>sessionStorage<\/code> into the new tab.<\/li>\n\n\n\n<li>Closing a tab\/window ends the session and clears objects in <code>sessionStorage<\/code>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Last week, we covered the lifecycle of Angular applications and how services can be used as a cache for our data. I mentioned that Angular apps are independent applications running in a browser tab and that closing such a tab &#8220;kills&#8221; the application and frees the memory used by our app &#8211; thus losing any [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-1195","post","type-post","status-publish","format-standard","hentry","category-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1195"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1195\/revisions"}],"predecessor-version":[{"id":1201,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1195\/revisions\/1201"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}