Skip to content

Set and get values with localStorage

localStorage, introduced with HTML 5, allows to store values, but can, other than cookies, storage large amounts of data and is more secure. To interact with it, there are two critical methods that are needed to interact with it:

Please note that localStorage stores it values as strings and that you need to convert your data to strings. See the following posts for how to do that:
JSON object to string
JSON string to object

Retrieve an item from localStorage:

var userString = localStorage.getItem('user');

Set an item in localStorage:

var userString = "{'firstName': 'John', 'lastName': 'Doe'}";
localStorage.setItem('user', userString);


Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.