You can store, retrieve and delete data using local storage built-in methods setItem()
, getItem()
, and removeItem()
.
Store
localStorage.setItem('color', 'green')
// OR
localStorage.color = 'green'
Retrieve
localStorage.getItem('color')
// OR
localStorage.color
Remove
localStorage.removeItem('color')
// OR
delete localStorage.color
Storage for Each Domain
You can test and confirm that your newly added property doesn’t exist on another site by navigating to a different site and entering the same lines again in the console. This should return null
because we’re in a different domain, and every domain gets its own local storage object – that’s a good thing. That means localStorage data set for a website can’t be read by other websites.
Once you save data on that domain, it stays there even if you close the browser!