Local —
storage
bar] ol ste2/8
il
ee
The localStorage object allows you to
save key/value pairs in the browser.
Stores data with no expiration date.,
The data is not deleted when the browser
is closed
, and is available
for future sessions.
Both, localStorage and sessionStorage
are mechanisms of Web Storage API.
The data is stored in an SQLite file in a
subfolder in user's profile3/8
localStorage can be used with
5 methods:
1 setItem()
Allows you to store values in the
localStorage object; accepts 2
parameters: a key, a value.
window. localStorage.setItem( ‘username’,
*CodewithSloba' ); ~
t
Na\[8 ad
Key can be referenced later to fetch
value attached to it.
il
Swipe —vA)
In case of arrays or objects, you have to
first convert them into strings using
JSON. stringify()
As localStorage can only store strings.
[x x
const creds = {
username = "CodewithSloba",
pw = "followForMore"
i
window. LocalStorage.setItem('login',
JSON. stringify(creds));
Swipe —
hy)5/8
2 getItem()
Allows you to access the data stored in
localStorage.
Simply pass key as the parameter and it
will return value accordingly.
window. LocalStorage.getItem( ‘login’ );
// {"username":"CodewithSloba",
"pw":"followForMore" }
Use JSON. parse() to convert it back to
object.
JSON. parse(window. localStorage.getItem( ‘login’ ));
hy)
Swipe —OE)
3 removeItem()
Allows you to remove the data stored in
localStorage.
Simply pass key as the parameter and it
will remove both, the key and value.
window. LocalStorage. removeltem( ‘login’ );
4 clear()
Deletes every item in the localStorage.
window. lLocalStorage.clear( );
hy)
Swipe —7/8
hy)
3. key()
Allows you to loop through the keys and
pass an index as the parameter to
retrieve the name of the key.
var keyName = window. localStorage.key( index);
Neal ed a
No data protection is assured by the browser hence
dont store sensitive information.
Size of localStorage is SMB and localStorage
operations are synchronous.
Swipe —Slobodan Gajié
Content Creator
oe
baal o\