Window.localStorage or simply
localStorage stores data forever until it is cleared by the application. This is different than
sessionStorage where the storage is cleared when browser is closed. Also, the data remain in sync if the user has multiple tabs open of the same application.
Let’s explore various methods that are available under
localStorage.setItem() method allows storing data in key & value pairs where both key and value should be in String format. To store JSON object in local storage first we need to convert that in a string using
To access data from
localStorage we can use method
getItem() method accepts key in string format as argument. If the key is not found
null is returned
localStorage item we can use method
removeItem() method accepts key in string format as an argument.
To clear local storage we can use
clear() method. This will clear all the local storage items.