Local Storage in JavaScript

Local Storage in JavaScript persists data locally, enabling enhanced, dynamic user experiences across sessions.

local-storage-in-javascript

Local storage in JavaScript

QUICK READ:

The localstorage object allows you to save key/value pairs in the browser. Stores data with no expiration date., (unlike sessionStorage object which stores data for a session). The data is not deleted when the browser is closed (unlike the sessionStorage object which deletes the data) 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 the user's profile.

localStorage can be used with 5 methods:

1. setItem() Allows you to store values in the localStorage object; accepts 2 parameters: a key, and a value. The key can be referenced later to fetch value attached to it. In case of arrays or objects, you have to first convert them into strings using JSON.stringify() As localStorage  can only store strings

2. getItem() Allows you to access the data stored in localStorage. Simply pass key as the parameter and it will return value accordingly. Use JSON.parse() to convert it back to object.

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. (Does nothing when parameter is left empty)

4. clear() Deletes every item in the localStorage.

5. key() Allows you to loop through the keys and pass an index as the parameter to retrieve the name of the key.

REMEMBER:

No data protection is assured by the browser hence dont store sensitive Information. Size of localStorage is SMB and localStorage operations are synchronous.

Like, Share and Subscribe #DevTools99 for more useful videos, tools info and #tutorials . Thank you!


DevTools99

Developer Co-Team

Let us make it easier for developers throughout the world to breathe. Let's keep things simple. Let's take it easy.