LocalStorage and SessionStorage in JavaScript: Store Data Safely
Learn how to store and read data in the browser with localStorage and sessionStorage, and pick the safer option for your next JavaScript project.

Contents

JavaScript localStorage and sessionStorage
JavaScript localStorage and sessionStorage offers two important built-in features for storing data in web browsers: localStorage and sessionStorage. Both features are used to store data in the browser’s memory. The main difference between them is how long the data is kept.
localStorage is a storage area where data is kept even after the browser is closed. For example, when a user visits a website and enters their username and password, this information can be stored in localStorage. When the user closes the browser and opens it again, you’ll see that this information is still there.
The following example code shows how to save data to localStorage:
localStorage.setItem("username", "JohnDoe");
localStorage.setItem("password", "password123");
This code saves the values “JohnDoe” and “password123” under the “username” and “password” keys. To retrieve this data, you can use the following code:
var username = localStorage.getItem("username");
var password = localStorage.getItem("password");
sessionStorage is used to store data in the browser similarly to localStorage, but the data is valid only for the tab that is currently open.
For example, when a user visits a web page and enters their username and password, this information is stored in sessionStorage, but when the user closes that tab, the information is lost. The following code shows how to save data to sessionStorage:
sessionStorage.setItem("username", "JohnDoe");
sessionStorage.setItem("password", "password123");
This code saves the values “JohnDoe” and “password123” under the “username” and “password” keys. To retrieve this data, you can use the following code:
var username = sessionStorage.getItem("username");
var password = sessionStorage.getItem("password");
In summary, localStorage and sessionStorage are presented as two built-in features you can use to store data in web browsers with JavaScript. localStorage is a storage area where data is kept even after the browser is closed, so users retain their information even after closing their browsers. sessionStorage, on the other hand, is a storage area where data is valid only for the tab that is currently open. This means the data is lost when you close the web page. This article has shown how to use localStorage and sessionStorage with example code and explanations.
Data storage methods such as JavaScript localStorage and sessionStorage are useful for web applications. However, you need to pay attention to some security points. In particular, it’s important to remember that data stored with these methods is stored unencrypted. Therefore, you need to encrypt the data to store it securely.
Also, localStorage and sessionStorage store data in the browser’s memory, so there is a possibility that a user can read the browser’s memory or change the data. Therefore, I recommend that the data stored with these methods should not be important or confidential.
The following code snippets will help you delete this data:
localStorage.removeItem("username");
sessionStorage.removeItem("password");
Finally, if these methods are not used securely, they pose a security risk.
How do you delete all the data saved in localStorage?
To delete all data saved in JavaScript localStorage, you can use the localStorage.clear() method. This method deletes all data in localStorage and completely removes it from the browser’s memory. For example:
localStorage.clear();
This code deletes all data in localStorage and completely removes it from the browser’s memory. With this method, you can delete all saved data at once. If you want to delete only specific data, you can use the localStorage.removeItem(key) method; the key variable is the key of the data you want to delete. Example:
localStorage.removeItem("username");
How do you update the value for a specific key in localStorage?
localStorage.setItem(key, value) method. This method replaces the current value for the specified key with a new value. For example:localStorage.setItem("username", "JaneDoe");
This code updates the current value for the “username” key to “JaneDoe”. If you need the old version of the data being updated, you can first get the old value and use it for the update. For example:
var oldUsername = localStorage.getItem("username");
localStorage.setItem("username", "JaneDoe");
console.log(oldUsername);
- MDN Web Docs – LocalStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
- MDN Web Docs – SessionStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
Share it if it helped


