Photo by Caspar Camille Rubin on Unsplash
The browser's localStorage
Storage at the browser side with atleast 5mb space
Introduction
The Web storage api is a set of mechanisms that enable browsers to store key-value pairs. Before HTML5, application data had to be sorted in cookies, included in every server request. Its intended to be far more user-friendly than using cookies.
Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.
There are 2 types of web storage,
- Local Storage
- Session Storage
We already have cookies. Why additional objects?
Unlike cookies, web storage objects are not sent to server with each request. Because of that, we can store much more. Most modern browsers allow at least 5 megabytes of data (or more) and have settings to configure that. Also unlike cookies, the server can’t manipulate storage objects via HTTP headers. Everything’s done in JavaScript.The storage is bound to the origin (domain/protocol/port triplet). That is, different protocols or subdomains infer different storage objects, they can’t access data from each other.
In this guide, you will learn/refresh about LocalStorage.
LocalStorage
The localStorage is property of the window (browser window object) interface allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.
- Data is kept for a longtime in local storage (with no expiration date.). This could be one day, one week, or even one year as per the developer preference ( Data in local storage maintained even if the browser is closed).
- Local storage only stores strings. So, if you intend to store objects, lists or arrays, you must convert them into a string using JSON.stringfy()
- Local storage will be available via the window.localstorage property.
- What’s interesting about them is that the data survives a page refresh (for sessionStorage) and even a full browser restart (for localStorage).
Main methods of LocalStorage
setItem(key, value)
Functionality: Add key and value to localStorage. setItem returns undefined. Every key, value pair stored is converted to a string. So it’s better to convert an object to string using JSON.stringify() prior to storing it in local storage.
Examples:
For a simple key, value strings.
// setItem normal strings window.localStorage.setItem("name", "goku");
Inorder to store objects, we need to convert them to JSON strings using JSON.stringify()
// Storing an Object without JSON stringify
const data = {
"commodity":"apple",
"price":43
};
window.localStorage.setItem('commodity', data);
var result = window.localStorage.getItem('commodity');
console.log("Retrived data without jsonified, "+ result);
getItem(key)
Functionality: This is how you get items from localStorage. If the given key is not present then it will return null.
Examples Get a simple key value pair
// setItem normal strings
window.localStorage.setItem("name", "goku");
// getItem
const name = window.localStorage.getItem("name");
console.log("name from localstorage, "+name);
removeItem(key)
Functionality: Remove an item by key from localStorage. If the given key is not present then it wont do anything.
Examples:
- After removing the value will be null.
// remove item window.localStorage.removeItem("commodity"); var result = window.localStorage.getItem('commodity'); console.log("Data after removing the key "+ result);
clear()
Functionality: Clears the entire localStorage Examples:- Simple clear of localStorage
// clear window.localStorage.clear(); console.log("length of local storage - after clear " + window.localStorage.length);
length
Functionality: We can use this like the property access. It returns number of items stored. Examples:
//length
console.log("length of local storage " + window.localStorage.length);
Codepen Tryout
Give a try in the below codepen.
When to use Local Storage
- Data stored in Local Storage can be easily accessed by third party individuals.
- So its important to know that any sensitive data must not sorted in Local Storage.
- Local Storage can help in storing temporary data before it is pushed to the server.
- Always clear local storage once the operation is completed.
localStorage browser support
localStorage as a type of web storage is an HTML5 specification. It is supported by major browsers including IE8. To be sure the browser supports localStorage, you can check using the following snippet:
if (typeof(Storage) !== "undefined") {
// Code for localStorage
} else {
// No web storage Support.
}
Where the local storage is saved ?
Windows
Firefox: C:\Users\\AppData\Roaming\Mozilla\Firefox\Profiles\\webappsstore.sqlite, %APPDATA%\Mozilla\Firefox\Profiles\\webappsstore.sqlite
Chrome: %LocalAppData%\Google\Chrome\User Data\Default\Local Storage\
Linux
- Firefox: ~/.mozilla/firefox//webappsstore.sqlite
- Chrome: ~/.config/google-chrome/Default/Local Storage/
Mac
- Firefox: ~/Library/Application Support/Firefox/Profiles//webappsstore.sqlite, ~/Library/Mozilla/Firefox/Profiles//webappsstore.sqlite
- Chrome: ~/Library/Application Support/Google/Chrome//Local Storage/, ~/Library/Application Support/Google/Chrome/Default/Local Storage/
Simple handson
Source Code: github
Limitations:
- Do not store sensitive user information in localStorage
- It is not a substitute for a server based database as information is only stored on the browser
- localStorage is limited to 5MB across all major browsers
- localStorage is quite insecure as it has no form of data protection and can be accessed by any code on your web page
- localStorage is synchronous, meaning each operation called would only execute one after the other