What is Local Storage in browsers and How can you use it?

A developer, geek, enthusiast, who loves to solve problems and fix things with technology.I am working on 💻frontend web development with Javascript and I love contributing to 🌟 open source.
Search for a command to run...

A developer, geek, enthusiast, who loves to solve problems and fix things with technology.I am working on 💻frontend web development with Javascript and I love contributing to 🌟 open source.
No comments yet. Be the first to comment.
Recently I have been working on migrating an old React application based on react-scripts to support Node.js 18. To make it work, I came across multiple npm tricks that helped solve these issues, so I am going to explain and document them in this blo...
Over the years I have picked up a lot of GitHub tricks, ways that I work around GitHub. I mostly use some set of commands and want to make a list of it & have it for reference. Setting Up SSH: I think anyone who is working with Git & GitHub should se...
The terminal is essential software for any developer or engineer. It is one of their daily drivers, so a terminal with better tools will not only save you time but also increase productivity. These are some key features that make Atuin stand out in y...

I have worked mostly in small & early-stage startups but got an opportunity to work at a large organization like Amazon. Listing down the different processes that are followed in both the organizations describing the development process & release pro...

2023 has been a rollercoaster ride for me. I faced a layoff early in the year from my last company, Hootboard, but bounced back and joined Amazon. Even though it is a contract role, it was the best decision I made. I traveled to the React Nexus confe...

The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions. localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends that is when the page is closed.
The simplest way to access local storage from our browser local like this
window.localStorage
which returns an object with the data stored in our browser's local storage.
The type of data you want to store in local storage should be key-value pairs which are always in the UTF-16 DOMString format, which uses two bytes per character. If you want to store objects and arrays they can also be stored by converting them into strings.
Methods available on storage object:
window.localStorage.setItem("mode","dark");
Using the above method you can set a key to a value, here I am setting mode to a value called dark.
window.localStorage.getItem("mode");
Using the above method you can get the value of the mode key that we previously set in the storage object.
window.localStorage.removeItem('mode');
Using this we can remove the value of the particular key from the storage object.
window.localStorage.clear();
Using this method we can remove all the key-value pairs from the storage object. This can use in cases where the user logs out from the application.
let user = {
name: "sridhar",
city: "Hyderabad"
}
or
let numbers = [1,2,3,4,5]
To store these kinds of object user or numbers array, you have to convert them to strings by JSON.stringify method.
const User = JSON.stringify(user);
window.localStorage.setItem('user',User);
JSON.parse(window.localStorage.getItem("user"));
MDN Docs links:
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
https://developer.mozilla.org/en-US/docs/Web/API/DOMString
If you have any doubts related to this article or anything related to tech or software engineering in general, drop a comment here or you can message me here sridhar katta