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.
To put items into the localStorage
we use setItem()
method that takes two arguments: key and value.
xxxxxxxxxx
1
localStorage.setItem('itemKey', 'itemValue');
Note:
localStorage
is BOM (Browser Object Model) global object so we can also access it usingwindow.localStorage
.
In order to store arrays or objects, you have to convert them to strings.
xxxxxxxxxx
1
const item = {
2
key: 'itemKey',
3
value: 'itemValue',
4
}
5
6
localStorage.setItem('item', JSON.stringify(item));