π Local Storage vs. Session Storage

Welcome to my blog ππ½
I'm a Front-End Developer with a passion for learning!
I write about Programming π©π½βπ» and Productivity Tips β
Search for a command to run...

Welcome to my blog ππ½
I'm a Front-End Developer with a passion for learning!
I write about Programming π©π½βπ» and Productivity Tips β
Thanks for reading!
Just a same topic. https://shamaayil.hashnode.dev/web-local-storage-ckff0pr1x035y58s1h1l95rva
Although my experience is primarily with front-end development, my current job is affording me the opportunity to do more full-stack work. Naturally, this meant that I needed to explore the different database GUIs available on the market. Here is a l...

A Note on the Series: Fast Fridays π is a series where you will find fast, short, and sweet tips/hacks that you may or may not be aware of. I will try to provide these (fairly) regularly on Fridays. In this Fast Friday tip, I'll explain how to crea...

Over the past year, Iβve started incorporating a Git GUI into my workflow. I still reach for the terminal about 90% of the time, but Iβve found that a GUI can really shine when it comes to things like viewing diffs, checking stashes, or resolving mer...

Simplifying Branch Management with Git Worktree

With all the AI tools popping up these days, I figured I'd try out some speech-to-text software in an attempt to boost my productivity. I've been hearing a lot about βdeveloping at the speed of thoughtβ, so here's a list of the top speech-to-text sof...

This article will cover the concepts of localStorage and sessionStorage, the differences between the two, and the main methods used when working with them. The nice part about these storage options is that they are pretty straightforward to use once you learn the API.
Both localStorage and sessionStorage give you the ability to store information locally in the browser via key-value pairs. The data stored for either is specific to the origin. This means that information stored on one site will not be accessible to a site with a completely different domain.
You can easily view local and session storage from your browser's Developer tools.
CodeSandbox Demo of Local Storage
localStorage has no expiration date and will persist until you manually clear it out of your browser. This can include anything from clearing the cache to manually going in and deleting the data via the developer tools.
On the other hand, sessionStorage will be removed once a page session ends via closing your browser or tab. Due to this limitation, sessionStorage is seldom used in comparison to localStorage.
Other than the differences in expiration, the API is exactly the same for both.
To add data to storage, you set key-value pairs using the setItem('key', 'value') method and the values that you pass in must be strings. You can also set more complex data types like Arrays and Objects, but you will have to convert them to a string using JSON.stringify().
/*Add an item to localStorage (Primitive)*/
localStorage.setItem('musician', 'Stan Getz');
/*Add an item to sessionStorage (Primitive)*/
sessionStorage.setItem('instrument', 'Tenor Saxophone');
/*Add an object to localStorage*/
localStorage.setItem('musicianInfo',
JSON.stringify({ name: 'Paul Desmond', instrument: 'Alto Saxophone' })
);
You can remove an item from storage via the removeItem(keyName) method. This method accepts a string of the name of the key you want to remove.
localStorage.removeItem('musician');
sessionStorage.removeItem('musicianInfo');
You can retrieve an item from storage via the getItem(keyName) method. Like removeItem(), this method also accepts a string of the name of the key you want to interact with.
When you need to un-stringify an object that was stored, you can use JSON.parse() to parse the string and convert it back into an object.
const musician = sessionStorage.getItem('musician');
/*Parse out object from JSON string */
const musicianInfo = JSON.parse(localStorage.getItem("musicianInfo"))
To remove all items from storage, use the clear() method.
localStorage.clear();
sessionStorage.clear();
With just a few methods, you can quickly get up and running with local and session storage. Since the data stored is easily accessible from the browser, you should only consider reaching for these storage options if the data you want to persist contains non-sensitive information.
If you enjoy what you read, feel free to like this article or subscribe to my newsletter, where I write about programming and productivity tips.
As always, thank you for reading and happy coding!
[Photo by Maksym Kaharlytskyi on Unsplash]