<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Web Storage]]></title><description><![CDATA[Web Storage]]></description><link>https://webstorage-explain.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 13:24:25 GMT</lastBuildDate><atom:link href="https://webstorage-explain.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The Basics of Web Storage: What You Need to Know]]></title><description><![CDATA[What is Web Storage?
Web Storage is a feature provided by modern web browsers that allows websites and web applications to store data directly in the browser – on the client-side, not on the server. It provides a simple and more secure alternative to...]]></description><link>https://webstorage-explain.hashnode.dev/the-basics-of-web-storage-what-you-need-to-know</link><guid isPermaLink="true">https://webstorage-explain.hashnode.dev/the-basics-of-web-storage-what-you-need-to-know</guid><dc:creator><![CDATA[Pritam Nandy]]></dc:creator><pubDate>Thu, 03 Jul 2025 16:05:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1751557922462/944aff4d-51c9-4abc-8304-7c910271cf6a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-what-is-web-storage"><strong>What is Web Storage?</strong></h3>
<p>Web Storage is a feature provided by modern web browsers that allows websites and web applications to store data directly in the browser – on the client-side, not on the server. It provides a simple and more secure alternative to using cookies for persisting data in the browser.</p>
<h3 id="heading-types-of-web-storage"><strong>Types of Web Storage</strong></h3>
<p>There are two types of Web Storage:</p>
<ol>
<li><p><strong>Local Storage</strong></p>
</li>
<li><p><strong>Session Storage</strong></p>
</li>
</ol>
<hr />
<h3 id="heading-why-web-storage">Why Web Storage?</h3>
<p>Before Web Storage, <strong>cookies</strong> were the primary method for storing data in the browser. However, they had several limitations:</p>
<ul>
<li><p><strong>Small storage size (~4 KB):</strong> Not suitable for storing large amounts of data.</p>
</li>
<li><p><strong>Included in every HTTP request:</strong> This slowed down page load times and increased unnecessary data transfer.</p>
</li>
<li><p><strong>Designed for server communication:</strong> Cookies were mainly used to send data to the server, not ideal for client-side storage.</p>
</li>
</ul>
<p><strong>Web Storage solves these issues by:</strong></p>
<ul>
<li><p><strong>Providing more space (5–10 MB):</strong> Ideal for storing more data like settings, preferences, or cached content.</p>
</li>
<li><p><strong>Client-side only storage:</strong> Data stays in the browser and is not sent to the server automatically.</p>
</li>
<li><p><strong>Faster performance:</strong> Since it doesn't affect HTTP requests, the app runs more efficiently.</p>
</li>
</ul>
<hr />
<h2 id="heading-local-storage">LOCAL STORAGE</h2>
<h3 id="heading-what-is-local-storage"><strong>What is Local Storage ?</strong></h3>
<p><strong>Local Storage</strong> is a type of web storage that allows you to save key/value data <strong>with no expiration time</strong>. The data stored remains even after the user closes the browser or shuts down the system — unless it is explicitly cleared.</p>
<p><strong>Key Features:</strong></p>
<ul>
<li><p>Data persists <strong>indefinitely</strong> (until cleared manually).</p>
</li>
<li><p>Stored per domain — accessible across all pages of the same site.</p>
</li>
<li><p>Typically allows around <strong>5–10 MB</strong> of storage.</p>
</li>
<li><p>Only supports <strong>string</strong> data (you can use JSON.stringify( ) and JSON.parse( ) for objects).</p>
</li>
</ul>
<p><strong>Use Cases :</strong> Some common use cases for local storage include:</p>
<ul>
<li><p>Caching authentication tokens for your app's API</p>
</li>
<li><p>Storing user preferences or settings for your app</p>
</li>
<li><p>Saving form data so users don't lose their input if they accidentally close the tab</p>
</li>
<li><p>Keeping track of cached data or images to improve performance</p>
</li>
</ul>
<hr />
<h2 id="heading-session-storage">SESSION STORAGE</h2>
<h3 id="heading-what-is-session-storage"><strong>What is Session Storage ?</strong></h3>
<p><strong>Session Storage</strong> stores data for the duration of the page session. The data is <strong>cleared automatically when the tab or browser is closed</strong>. It is ideal for temporary data that doesn’t need to persist.</p>
<p><strong>Key Features:</strong></p>
<ul>
<li><p>Data is kept only during the <strong>current session</strong>.</p>
</li>
<li><p><strong>Tab-specific</strong> — not shared across browser tabs or windows.</p>
</li>
<li><p>Similar API to localStorage.</p>
</li>
<li><p>Also limited to <strong>5 MB</strong> typically.</p>
</li>
</ul>
<p><strong>Use Cases :</strong> Session Storage is applicable for cases where you want to store temporary data just for a single user session, like:</p>
<ul>
<li><p>Storing a shopping cart between pages before checkout.</p>
</li>
<li><p>Storing form data between pages in a multi-step process.</p>
</li>
<li><p>Storing user preferences or settings for a single session.</p>
</li>
</ul>
<hr />
<h2 id="heading-explanation-of-local-and-session-storage-with-example"><strong>Explanation of Local and Session Storage with example</strong></h2>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Web Storage Demo<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Web Storage Example<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"inputData"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter what you want to store"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"saveToLocal()"</span>&gt;</span>Save to Local<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"saveToSession()"</span>&gt;</span>Save to Session<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"showStorage()"</span>&gt;</span>Show Storage<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"output"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">saveToLocal</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">const</span> value = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"inputData"</span>).value;
        <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"localData"</span>, value);
        alert(<span class="hljs-string">"Saved to Local Storage"</span>);
      }

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">saveToSession</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">const</span> value = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"inputData"</span>).value;
        sessionStorage.setItem(<span class="hljs-string">"sessionData"</span>, value);
        alert(<span class="hljs-string">"Saved to Session Storage"</span>);
      }

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">showStorage</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">const</span> local = <span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"localData"</span>);
        <span class="hljs-keyword">const</span> session = sessionStorage.getItem(<span class="hljs-string">"sessionData"</span>);
        <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"output"</span>).innerText =
          <span class="hljs-string">`Local: <span class="hljs-subst">${local || <span class="hljs-string">'Empty'</span>}</span> | Session: <span class="hljs-subst">${session || <span class="hljs-string">'Empty'</span>}</span>`</span>;
      }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Output : Local Storage</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751556290031/069dc9de-93f7-472b-87d9-892e3c9f82ea.png" alt class="image--center mx-auto" /></p>
<p>Session Storage</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751556340581/bb94bc02-9d68-4ed9-b5d9-91ff36931fb3.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-note"><strong><em>Note</em></strong> <em>:</em></h3>
<p><mark>How to find application tab!! </mark> <em><mark>Inspect → On the top Click “Application” → Check Storage (Local storage / Session storage)</mark></em></p>
<hr />
<h3 id="heading-difference-between-local-storage-and-session-storage"><strong>Difference between Local Storage and Session Storage</strong></h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Local Storage</td><td>Session Storage</td></tr>
</thead>
<tbody>
<tr>
<td>Persistence</td><td>Until manually cleared</td><td>Until tab/window is closed</td></tr>
<tr>
<td>Scope</td><td>Shared across tabs (same origin)</td><td>Only accessible in the same tab</td></tr>
<tr>
<td>Size Limit</td><td>~5–10 MB</td><td>~5 MB</td></tr>
<tr>
<td>Use Case Examples</td><td>Theme, preferences, cart</td><td>Form data, session tokens</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-cookies"><strong>COOKIES</strong></h2>
<h3 id="heading-what-is-cookies"><strong>What is cookies?</strong></h3>
<p><strong>Cookies</strong> are small pieces of data (typically less than 4 KB) that are stored in the user's browser and sent to the <strong>server with every HTTP request</strong>. They are mainly used for maintaining <strong>sessions</strong>, <strong>authentication</strong>, and <strong>tracking</strong> user activity across different pages or visits.</p>
<p>Compared to other forms of web storage like localStorage and sessionStorage, cookies have some limitations:</p>
<ul>
<li><p>Storage space is very small, usually around 4KB.</p>
</li>
<li><p>They are automatically sent to the server with every HTTP request, which can impact performance.</p>
</li>
<li><p>Managing and accessing cookies through JavaScript is more cumbersome than the simple key-value APIs of localStorage and sessionStorage.</p>
</li>
</ul>
<p>Cookies are commonly used for:</p>
<ul>
<li><p>Session management and authentication</p>
</li>
<li><p>User preferences like themes</p>
</li>
<li><p>Tracking and analytics</p>
</li>
</ul>
<hr />
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p><strong>Local Storage</strong>, <strong>Session Storage</strong>, and <strong>Cookies</strong> each serve different purposes:</p>
<ul>
<li><p>Use <strong>Local Storage</strong> for long-term data like preferences.</p>
</li>
<li><p>Use <strong>Session Storage</strong> for temporary, tab-specific data.</p>
</li>
<li><p>Use <strong>Cookies</strong> when you need to send data to the server, like in authentication.</p>
</li>
</ul>
<p>Choose based on your data’s <strong>lifespan</strong>, <strong>scope</strong>, and <strong>security needs</strong>.</p>
]]></content:encoded></item></channel></rss>