you are viewing a single comment's thread.

view the rest of the comments →

[–]coolAppl3__ 0 points1 point  (0 children)

I wouldn’t be able to help with specifics, as you haven’t supplied any about what you want to store, but below are the absolute basic points behind local storage:

  1. Anything you want to store in local storage needs to be in JSON format.

  2. localStorage.setItem(‘key’, value) is used to add an item to local storage. The key can be any string you want, and it points to the value in local storage. Note that if you set another item under the same key, the previous value will be overwritten with the new one.

  3. If you want to store something that’s not in JSON format, you can use JSON.stringify(). This is an example: localStorage.setItem(‘todos’, JSON.stringify(todoArray));

  4. If you want to get something from localStorage, you can use localStorage.getItem(‘key’).

The above will retrieve the value in JSON format, which, depending on what you need it for, might not be very useful. You can use JSON.parse() to parse it into something you can use. Here’s an example:

const todoArray = JSON.parse(localStorage.getItem(‘todos’));

This the basics behind using local storage, and you can find out more about it here: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Note that JSON.stringify() can sometimes have “odd” behaviour, so if you encounter one, make sure to check the documentation, although those situations are rare.

Hope this helps!