The read-only localStorage
property allows you to access a Storage
object for the Document
's origin; the stored data is saved across browser sessions. localStorage
is similar to sessionStorage
, except that while data stored in localStorage
has no expiration time, data stored in sessionStorage
gets cleared when the browsing session ends—that is, when the browser is closed.
It should be noted that data stored in either localStorage
or sessionStorage
is specific to the protocol of the page.
Syntax
myStorage = window.localStorage;
Value
A Storage
object which can be used to access the current origin's local storage space.
Exceptions
SecurityError
- The request violates a policy decision, or the origin is not a valid scheme/host/port tuple (this can happen if the origin uses the
file:
or data:
scheme, for example). For example, the user may have their browser configured to deny permission to persist data for the specified origin.
Example
The following snippet accesses the current domain's local Storage
object and adds a data item to it using Storage.setItem()
.
localStorage.setItem('myCat', 'Tom');
The syntax for removing the localStorage item is as follows:
localStorage.removeItem("myCat");
Note: Please refer to the Using the Web Storage API article for a full example.
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'localStorage' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
localStorage | 4 | (Yes) | 3.5 | 8 | 10.50 | 4 |
sessionStorage | 5 | (Yes) | 2 | 8 | 10.50 | 4 |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 2.1 | (Yes) | ? | 8 | 11 | iOS 3.2 |
All browsers have varying capacity levels for both localStorage
and sessionStorage
. Here is a detailed rundown of all the storage capacities for various browsers.
Note: Starting with iOS 5.1, Safari Mobile stores localStorage
data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short. Safari Mobile's Private Browsing mode also prevents writing to localStorage
entirely.