Member-only story

Learn about LocalStorage in JavaScript.

Javascript Jeep🚙💨
2 min readMay 29, 2023

--

Photo by JOSHUA COLEMAN on Unsplash

What is localStorage?

localStorage is a web storage API that allows JavaScript code to store data in the user’s browser. The data is stored in key-value pairs, and it is not deleted when the browser is closed. This makes localStorage a good choice for storing data that needs to be persisted between page loads, such as user preferences or shopping cart contents.

How to use localStorage

To use localStorage, you first need to get a reference to the localStorage object. You can do this using the window.localStorage property.

Set data to localStorage

Once you have a reference to the localStorage object, you can use the setItem() method to store data. The setItem() method takes two arguments: the key and the value. The key is a string that identifies the data, and the value is the data itself. For example, the following code would store the value “12345” in the key “myNumber”:

window.localStorage.setItem(“myNumber”, “12345”);

Get data from localStorage

To retrieve data from localStorage, you can use the getItem() method. The getItem() method takes one argument: the key. The method returns the value associated with the key, or null if the key…

--

--

Javascript Jeep🚙💨
Javascript Jeep🚙💨

No responses yet