localStorage has at least 5MB of data storage across all major web browsers.
To add some data to local storage you need to:
If you want to retrieve data from local storage you need to:
const fruit = localStorage.getItem("fruit"); console.log(fruit); //prints 'apple';
Local storage only stores strings so if you want to store other types of data you need to convert it to JSON before:
localStorage.setItem("cart", JSON.stringify(["apple", "banana"]));
And to retrieve it you need to convert it from JSON:
const cart = JSON.parse(localStorage.getItem("cart")); console.log(cart) //prints ['apple', 'banana']
Find this. is in stackoverflow discussion.
localStorage works in general?
There are five ways to use
localStorage in web applications:
setItem(): Adds key and value
getItem(): Gets items from
removeItem(): Removes an item by key
clear(): Clears all
key(): Passes a number to retrieve the key
Are there any limitations in
Yes, there are some limitations. It may sound that it is easy to implement but it is also easy to misuse it. Things that you shouldn’t use in
- Never store any sensitive user information in
- There is a limit of 5MB in
- There is no data protection in
localStoragecan be accessed by any code on web page