How to use Local Storage in JavaScript?

JavaScript

 📅  04 August, 2019 |  🕕  2 min |   Share

Local storage allows us to store the data in the user's browser. The data stored will not be deleted even after the browser is closed.

The data will be stored in the form of key/value pairs. Both keys and values are always strings.

Storing the data

For example, we have a user whose preferences/settings needs to be stored in the localStorage. Let's create a userPreferences object with the user settings.

const userPreferences = {
  darkMode: true,
  accentColor: 'red',
};

Now, we'll store it in the browser using localStorage.setItem method

localStorage.setItem('userData', JSON.stringfy(userPreferences));

Here userData is key and userPreferences is the value. The reason we are using JSON.stringfy method is to convert the userPreferences object to a string.

Retrieving the data

When we need to use the data, we can retrieve it using localStorage.getItem method by passing the key as the argument. The data returned will be in the form of a string, so we need to use JSON.parse method to convert it into an object.

let userData = localStorage.getItem('userData'); //"{ darkMode: true, accentColor:'red'}"

//convert the string to an object
userData = JSON.parse(userData);

console.log(userData); // { darkMode: true, accentColor: 'red'}

Clearing the data

If you want to clear all the data stored in the local storage, we need to use localStorage.clear method.

localStorage.clear();

Removing the particular data

If you want to remove data for a particular key, we need to use localStorage.removeItem method by passing key as the argument.

localStorage.removeItem("userData").

Session Storage

sessionStorage is same as localStorage . But the main difference is, the data stored will be cleared automatically when the user closes the web page. All the methods are also same.

Limitations

Do not store any sensitive data in the local storage as it can be accessed by any one browsing the page. And there is no form of data protection.

Example

If you're reading this on Dev.to, Open the developer tools by pressing ctrl(cmd) + shift + I and go to Application → Local Storage. There you'll find how Dev.to using local storage for storing user details with the key current_user.

If you're reading this on my blog, repeat the above steps and you'll see how I'm using local storage for persisting the dark mode using the key darkMode.

Thank You.

Reference

LocalStorage - MDN

Session Storage - MDN

Share
Getting started with CSS variablesNull vs Undefined in JavaScript in 1 min

Gopi Krishna

Hello, thanks for the read! If you found this article helpful, have constructive feedback, or just want to say hello, connect with me on twitter or shoot me an email. Thanks in advance!