{"id":8708,"date":"2025-03-22T17:40:16","date_gmt":"2025-03-22T12:10:16","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=8708"},"modified":"2025-03-22T17:40:20","modified_gmt":"2025-03-22T12:10:20","slug":"storage-in-javascript","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/storage-in-javascript\/","title":{"rendered":"Storage in JavaScript"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Storage:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Storage in JavaScript refers to the ability to save data on a user&#8217;s browser. This stored data can be accessed and used later, even after the user refreshes the page or revisits the website.<br>There are two main types of web storage: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>localStorage<\/li>\n\n\n\n<li>sessionStorage<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why Do We Use Storage?<\/strong><br>Storage is helpful for many reasons, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Remembering user preferences.<\/li>\n\n\n\n<li>Storing login sessions<\/li>\n\n\n\n<li>Caching data to reduce server request<\/li>\n\n\n\n<li>Saving form inputs to prevent data loss<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Without storage, every time a user refreshes the page or closes their browser, all that info would be lost. Storage solves that problem by keeping data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How does it work?<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>JavaScript gives us simple tools localStorage and sessionStorage &#8211; to save, retrieve, and delete data. <\/li>\n\n\n\n<li>They work like a key-value pair system. <\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Simple Syntax<\/strong>:<br>Here&#8217;s the basic syntax for localStorage(it&#8217;s the same for sessionStorage too):<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.18.33\u202fPM-1.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"774\" height=\"422\" data-id=\"8725\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.18.33\u202fPM-1.png\" alt=\"\" class=\"wp-image-8725\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.18.33\u202fPM-1.png 774w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.18.33\u202fPM-1-300x164.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.18.33\u202fPM-1-768x419.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.18.33\u202fPM-1-550x300.png 550w\" sizes=\"(max-width: 774px) 100vw, 774px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Save data<\/strong>: This stores the value under the key name.<\/li>\n\n\n\n<li><strong>Get data<\/strong>: This retrieves the value tied to the key.<\/li>\n\n\n\n<li><strong>Remove data<\/strong>: This deletes the data for that key.<\/li>\n\n\n\n<li><strong>Clear everything<\/strong>: This wipes out all stored data<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A simple example:<\/strong><br>A mini website where users can type their name, save it and see it displayed even after refreshing the page.<br>1. HTML structure:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.59.05\u202fPM-1.png\"><img decoding=\"async\" width=\"1024\" height=\"370\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.59.05\u202fPM-1-1024x370.png\" alt=\"\" class=\"wp-image-8729\" style=\"width:736px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.59.05\u202fPM-1-1024x370.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.59.05\u202fPM-1-300x108.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.59.05\u202fPM-1-768x278.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.59.05\u202fPM-1-830x300.png 830w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.59.05\u202fPM-1.png 1350w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Usage<\/strong>: This sets up the webpage<br>2.JavaScripe: Load saved name on page load<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.03.12\u202fPM.png\"><img decoding=\"async\" width=\"1024\" height=\"299\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.03.12\u202fPM-1024x299.png\" alt=\"\" class=\"wp-image-8730\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.03.12\u202fPM-1024x299.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.03.12\u202fPM-300x88.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.03.12\u202fPM-768x224.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.03.12\u202fPM-850x248.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.03.12\u202fPM.png 1116w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Usage: <\/strong>This runs when the page loads. It:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Check localStorage for a saved name under the key &#8220;userName&#8221;.<\/li>\n\n\n\n<li>If a name exists, it displays it in the &lt;span id=&#8221;savedName&#8221;&gt;.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">3. Javascript: Save name on button click<\/p>\n<\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.05.07\u202fPM-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"264\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.05.07\u202fPM-2.png\" alt=\"\" class=\"wp-image-8733\" style=\"width:715px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.05.07\u202fPM-2.png 1002w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.05.07\u202fPM-2-300x79.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.05.07\u202fPM-2-768x202.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-5.05.07\u202fPM-2-850x224.png 850w\" sizes=\"(max-width: 1002px) 100vw, 1002px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Usage: <\/strong>This runs when the &#8220;Save Name&#8221; button is clicked it<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Grabs the text typed in the input box(nameInput).<\/li>\n\n\n\n<li>Saves it in localStorage with the key &#8220;userName&#8221;.<\/li>\n\n\n\n<li>Updates the &lt;span id=&#8221;savedName&#8221;&gt; to show the name instantly.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.52.18\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"281\" data-id=\"8727\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.52.18\u202fPM-1024x281.png\" alt=\"\" class=\"wp-image-8727\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.52.18\u202fPM-1024x281.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.52.18\u202fPM-300x82.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.52.18\u202fPM-768x211.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.52.18\u202fPM-850x233.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-22-at-4.52.18\u202fPM.png 1428w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Even if we refresh the page, the name stays there because <strong>localStorage<\/strong> keeps it save!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Viewing and editing the storage:<\/strong><br>We can see the localStorage or sessionStorage using the browser&#8217;s developer tools.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Right-click on your webpage and click &#8220;Inspect&#8221;.<\/li>\n\n\n\n<li>Go to the &#8220;Application&#8221; tap(in chrome) or &#8220;storage&#8221; tab in(in Firefox)<\/li>\n\n\n\n<li>Look under &#8220;LocalStorage&#8221; or &#8220;SessionStorage&#8221; to see your saved data. We can edit or delete it here!<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">&#8211;&gt; <strong>Storage Limits: <\/strong>Local and session storage usually hold about 5-10MB of data. Cookies are much smaller(around 4 KB).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8211;&gt; <strong>Strings Only: <\/strong>These methods save data as text. If you need to save numbers or lists, you will need to use <strong>JSON.stringfy() <\/strong>and <strong>JSON.parse<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Storage: Storage in JavaScript refers to the ability to save data on a user&#8217;s browser. This stored data can be [&hellip;]<\/p>\n","protected":false},"author":513,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-8708","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2gs","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/513"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8708"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8708\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}