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.
Type: | Storage extends Object |
Constructor: | private |
Singleton: | localStorage |
Namespace: | global |
Direct subclasses: | None |
JSX Support: | No |
Examples
JavaScript
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: undefined
getItem(key)
Retrieves the value associated with the given key.
Parameter | Type | Description |
---|---|---|
key | string |
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 | Description |
---|---|---|
index | number |
Returns: string
removeItem(key)
Removes the given key/value pair from the storage.
Parameter | Type | Description |
---|---|---|
key | string |
Returns: undefined
setItem(key, value)
Stores a string value using the given key.
Parameter | Type | Description |
---|---|---|
key | string |
|
value | string |
Returns: undefined
Properties
length
The number of items in the storage.
Type: | number |
Settable: | No |
This property can only be set via constructor. Once set, it cannot change anymore.