Window localStorage
Example
Set and retrieve localStorage name/value pair:
localStorage.setItem("lastname", "Smith");
localStorage.getItem("lastname");
Try it Yourself »
More examples below.
Definition and Usage
The localStorage
object allows you to save key/value pairs in the browser.
Note
The localStorage object stores data with no expiration date.
The data is not deleted when the browser is closed, and are available for future sessions.
See Also:
The sessionStorage Object which stores data for one session.
(The data is deleted when the browser window is closed)
Syntax
window.localStorage
or just:
localStorage
Save Data to Local Storage
localStorage.setItem(key, value);
Read Data from Local Storage
let lastname = localStorage.getItem(key);
Remove Data from Local Storage
localStorage.removeItem(key);
Remove All (Clear Local Storage)
localStorage.clear();
Parameters
Parameter | Description |
key | Required. The name of a key. |
value | Required. The value of the key. |
Return Value
Type | Description |
An object | A localStorage object. |
More Examples
Count the number of times a user has clicked a button:
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount)
+ 1;
} else {
localStorage.clickcount = 1;
}
Try it Yourself »
Browser Support
window.localStorage
is supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |