Global object “localStorage”
The localStorage
object allows storing key-value pairs in a persistent store. Both keys and values are stored as strings.
On iOS, there is an additional object secureStorage
available in the global scope. This is a drop-in replacement for localStorage
that keeps data in the encrypted iOS Keychain.
The localStorage
is only meant to store relatively short strings. To store larger amounts of data it is recommended to use the FileSystem API.
Constructor | private |
Singleton | localStorage |
Namespace | global |
Direct subclasses | None |
JSX support | No |
Example
localStorage.setItem("message", "Hello World!");
console.log(localStorage.getItem("message")); // "Hello World!"
See also:
JSX A example how to read and write from the localStorage
Methods
clear()
Remove all key/value pairs from the storage.
Returns void
getItem(key)
Retrieves the value associated with the given key.
Parameter | Type | Optional | Description |
---|---|---|---|
key | string |
No |
Returns string
key(index)
Returns the name of the key at the given index in the storage. The order of keys is platform dependent, you should not rely on it to be deterministic
Parameter | Type | Optional | Description |
---|---|---|---|
index | number |
No |
Returns string
removeItem(key)
Removes the given key/value pair from the storage.
Parameter | Type | Optional | Description |
---|---|---|---|
key | string |
No |
Returns void
setItem(key, value)
Stores a string value using the given key.
Parameter | Type | Optional | Description |
---|---|---|---|
key | string |
No | |
value | string |
No |
Returns void
Properties
length
The number of items in the storage.
Type | number |
Settable | No |
Change events | No |
This property can only be set via constructor. Once set, it cannot change anymore.