DevDockTools

Browser Storage: Local and Session

Comprehensive guide to using browser local storage and session storage for senior web developers, including practical tips and real-world use cases

By Daniel Agrici4 min read
browser storagelocal storagesession storageweb developmentjavascript

Introduction to Browser Storage

Browser storage refers to the ability of a web application to store data locally on a user's device, allowing for faster and more efficient access to that data. There are two main types of browser storage: local storage and session storage. Local storage persists even after the browser is closed, while session storage is deleted when the session ends.

Local Storage

Local storage is a key-value store that allows you to store data as a string. It has a larger storage limit than session storage, typically around 5MB. Local storage is suitable for storing non-sensitive data such as user preferences, game state, or cached resources.

Storing Data in Local Storage

To store data in local storage, you can use the localStorage.setItem() method, which takes two arguments: the key and the value. For example:

localStorage.setItem('username', 'John Doe');

You can also store JSON data in local storage by using the JSON.stringify() method to convert the JSON object to a string, and then storing the string in local storage using the localStorage.setItem() method. For example:

const userData = { name: 'John Doe', age: 30 };
localStorage.setItem('user', JSON.stringify(userData));

To retrieve the data, you can use the localStorage.getItem() method, which returns the value associated with the given key. For example:

const username = localStorage.getItem('username');
console.log(username); // Output: John Doe

To retrieve JSON data, you can use the JSON.parse() method to convert the string back to a JSON object. For example:

const user = JSON.parse(localStorage.getItem('user'));
console.log(user); // Output: { name: 'John Doe', age: 30 }

Removing Data from Local Storage

To remove data from local storage, you can use the localStorage.removeItem() method, which takes one argument: the key. For example:

localStorage.removeItem('username');

You can also use the localStorage.clear() method to remove all data from local storage.

Session Storage

Session storage is similar to local storage, but it is deleted when the session ends. Session storage is suitable for storing temporary data such as form data or shopping cart contents.

Storing Data in Session Storage

To store data in session storage, you can use the sessionStorage.setItem() method, which takes two arguments: the key and the value. For example:

sessionStorage.setItem('formData', 'name=John Doe&age=30');

You can also store JSON data in session storage by using the JSON.stringify() method to convert the JSON object to a string, and then storing the string in session storage using the sessionStorage.setItem() method. For example:

const formData = { name: 'John Doe', age: 30 };
sessionStorage.setItem('formData', JSON.stringify(formData));

To retrieve the data, you can use the sessionStorage.getItem() method, which returns the value associated with the given key. For example:

const formData = sessionStorage.getItem('formData');
console.log(formData); // Output: name=John Doe&age=30

To retrieve JSON data, you can use the JSON.parse() method to convert the string back to a JSON object. For example:

const formData = JSON.parse(sessionStorage.getItem('formData'));
console.log(formData); // Output: { name: 'John Doe', age: 30 }

Removing Data from Session Storage

To remove data from session storage, you can use the sessionStorage.removeItem() method, which takes one argument: the key. For example:

sessionStorage.removeItem('formData');

You can also use the sessionStorage.clear() method to remove all data from session storage.

Comparison of Local Storage and Session Storage

The following table compares the key features of local storage and session storage: | Feature | Local Storage | Session Storage | | --- | --- | --- | | Persistence | Persists even after browser is closed | Deleted when session ends | | Storage Limit | Typically around 5MB | Typically around 5MB | | Security | Not secure | Not secure | | Suitable for | Non-sensitive data | Temporary data |

Practical Tips and Real-World Use Cases

Local storage and session storage can be used in a variety of real-world scenarios, such as:

  • Storing user preferences, such as font size or color scheme
  • Caching resources, such as images or scripts
  • Storing form data, such as user input or shopping cart contents
  • Implementing a shopping cart or wishlist feature
  • Storing game state or high scores

To work with JSON data in local storage or session storage, you can use tools like the json-formatter or json-validator to format and validate your JSON data. You can also use the base64-encoder to encode and decode base64 strings.

Next, try using the json-formatter to format and validate your JSON data, and see how it can help you work more efficiently with local storage and session storage.

Frequently Asked Questions

What is the difference between local storage and session storage?
Local storage persists even after the browser is closed, while session storage is deleted when the session ends. Local storage has a larger storage limit than session storage.
How do I store JSON data in local storage?
You can store JSON data in local storage by using the `JSON.stringify()` method to convert the JSON object to a string, and then storing the string in local storage using the `localStorage.setItem()` method.
Can I use local storage to store sensitive data?
No, you should not use local storage to store sensitive data such as passwords or credit card numbers, as it is not secure. Instead, use a secure storage solution such as a server-side database or a secure token storage system.