The setItem(key, value)
method
is used to add a key-value pair
to the storage.
Both key and value must be strings.
In the example above, the localStorage.setItem
function is used to store the value tennis
with the key sport in the localStorage
.
Similarly, the value hiking with
the key activity is stored in the sessionStorage
.
If an item with the same key already exists,
the setItem
method will update it with the new value.
So, the value for the key sport will be updated to
cricket and the value for the key activity
will be updated to swimming.