A web browser that supports the localStorage and sessionStorage objects, such as Google Chrome, Mozilla Firefox, or Microsoft Edge. These objects are supported by most modern browsers, but you can check the compatibility of your browser on the Can I Use website (https://caniuse.com/#feat=namevalue-storage).
A text editor or integrated development environment (IDE) to write and edit your code. You can use any text editor or IDE that you are comfortable with, such as Visual Studio Code, Sublime Text, or Atom
Saving a value to localStorage
To save a value in local storage, you can use the
setItem() method like this:
Getting a value from localStorage
var value = localStorage.getItem('key')
Removing a value from localStorage
You can also use the
removeItem() method to remove a value from storage:
Saving, getting, and deleting a value from sessionStorage
sessionStorage object works in the same way, but the data is only stored for the current session and is deleted when the browser is closed.
sessionStorage.setItem('key', 'value') var value = sessionStorage.getItem('key') sessionStorage.removeItem('key')
Things to note when working with localStorage and sessionStorage
It's important to note that the values stored in browser storage are always strings, so you may need to convert them to a different data type if necessary.
For example, if you want to store a number, you can use the
parseFloat() functions to convert the string to a number.
localStorage.setItem('num', '42') var num = parseInt(localStorage.getItem('num'))
Further readign references for the topic of creating and reading from browser storage, you can refer to the official documentation for the localStorage and sessionStorage objects on the Mozilla Developer Network (MDN) website.
Here are the links to the documentation for each object:
- localStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
- sessionStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
Additionally, the World Wide Web Consortium (W3C) also provides a specification for web storage, which you can find here: https://www.w3.org/TR/webstorage/. This specification defines the standard for web storage and provides a detailed description of how it works. You can use this reference as a more technical resource for understanding the underlying concepts and principles of web storage.