EN
JavaScript - add item to localStorage
0
points
In this article, we would like to show you how to add items to localStorage in JavaScript.
Practical example
To put items into the localStorage we use setItem() method that takes two arguments: key and value.
// ONLINE-RUNNER:browser;
localStorage.setItem('itemKey', 'itemValue');
Note:
localStorageis BOM (Browser Object Model) global object so we can also access it usingwindow.localStorage.
Store arrays or objects
In order to store arrays or objects, you have to convert them to strings.
// ONLINE-RUNNER:browser;
const item = {
key: 'itemKey',
value: 'itemValue',
}
localStorage.setItem('item', JSON.stringify(item));