Introduction
The Web offers a variety of APIs to build your app or website. You could access using JavaScript code. These days browsers offer many APIs to developers. In the last article, we read about XMLHttpRequest API and in this article, we will understand Storage APIs available.
Before moving further let us look at the previous articles of the JavaScript series:
- Voice of a Developer: JavaScript Data Types - Part One
- Voice of a Developer: JavaScript Objects - Part Two
- Voice of a Developer: JavaScript Engines - Part Three
- Voice of a Developer: JavaScript Common Mistakes - Part Four
- Voice of a Developer: Editors - Part Five
- Voice of a Developer: VSCode - Part Six
- Voice of a Developer: Debugging Capabilities of VSCode - Part Seven
- Voice of a Developer: JavaScript OOP - Part Eight
- Voice of a Developer: JavaScript Useful Reserved Keywords - Part Nine
- Voice of a Developer: JavaScript Functions - Part Ten
- Voice of a Developer: JavaScript Functions Invocations - Part Eleven
- Voice of a Developer: JavaScript Anonymous Functions - Part Twelve
- Voice of a Developer: JavaScript Pure And Impure Function - Part Thirteen
- Voice of a Developer: JavaScript Closures - Part Fourteen
- Voice of a Developer: JavaScript Currying - Part Fifteen
- Voice of a Developer: JavaScript Chaining - Part Sixteen
- Voice of a Developer: JavaScript Array Methods - Part Seventeen
- Voice of a Developer: JavaScript ECMAScript 6 - Part Eighteen
- Voice of a Developer: JavaScript ECMAScript 6 Features v1 - Part Nineteen
- Voice of a Developer: JavaScript ECMAScript 6 Features v2 - Part Twenty
- Voice of a Developer: JavaScript Web Workers - Part Twenty One
- Voice of a Developer: JavaScript JSLint v1 - Part Twenty-Two
- Voice of a Developer: JavaScript JSLint v2 - Part Twenty Three
- Voice of a Developer: XMLHttpRequest API - Part Twenty Four
Before I dig into storage we will understand the key-value pairs.
KVP stores two values together. It is a set of two linked data items; a unique key and some value. This is frequently used in the hash, map, dictionary, JSON, configuration files, etc. Example – below the representation of KVP could be translated in the form of Object in JavaScript.
Key-Value pair (KVP)

- var obj = {width: 300, height:200, title: 'Menu'};
Now let’s proceed with Storage API
Web Storage API
- sessionStorage: maintains storage as long as the browser is open, including page reloads and restores. It gets clear when the session ends.
- localStorage: same thing like sessionStorage, but persists even when the browser is closed/opened again. This means it has no expiration time.
Note: There was a way in Firefox 9, i.e., globalStorage but it was deprecated after HTML5 introduced this local storage.
These are available via Window.sessionStorage & Window.localStorage. Both of these provide access to create, get, modify, delete client-side storage depending upon session or local store.
Generally, 5MB, 5MB is available for sessionStorage& localStorage respectively.
You can leverage utility to run these tests at your browser and it will show you the output. I conducted test at my browser Chrome 50 & Firefox 45 and both gave same result, i.e., 5 MB.
Please note 5MB does not mean 5 million characters are allowed. Developers think that each character occupies 1 byte, which is correct for some programming languages. But JavaScript occupies 2 bytes each character as it stores string on basis on UTF-16.
Write localStorage
Below code will write data into localStorage and using setItem() function:
Access client-side storage
Facts about client-side storage
How much storage does browser provide?
How can we check the available storage in our browser?

Misconception
localStorage
- <script>
- localStorage.setItem("fname", "Sumit");
- localStorage.setItem("lname", "Jolly");
- localStorage.setItem("profile", "http://www.c-sharpcorner.com/members/sumit-jolly");
- </script>
Configure a website in local Web server and run URL: http://localhost:8079/test.html


Read localStorage
Example
- (function() {
- for (var i = 0; i < localStorage.length; i++){
- console.log(localStorage.getItem(localStorage.key(i)));
- }
- })(); // self-invoking function will give below output
Jolly
sessionStorage
Cookies
A simple cookie can be created like:
- document.cookie = "fname=Sumit";
- document.cookie = "lname=Jolly";
- console.log(document.cookie); // fname=Sumit; lname=Jolly

- document.cookie = "fname=Sumit; expires=Mon, 15 Aug 2016 12:00:00 UTC";

| localStorage | sessionStorage | Cookies |
| Not added | Not added | Added to every HTTP request |
| No expiry | date Expire with session | Has both features i.e., session and you can define expiry date |
| Limit of 5MB | Limit of 5MB | Cookies give you a limit of 4096 bytes |
Rajib Das GuptaPosted Jun 29, 2016, 5:14 AM
Nice
Neeraj KumarPosted May 17, 2016, 2:59 PM
Good one
Kuppurasu NagarajPosted May 17, 2016, 10:53 AM
Nice sharing..
Debasis SahaPosted May 17, 2016, 9:47 AM
Nice one..
Guest UserPosted May 17, 2016, 8:37 AM
Sure, I'll take care in future to add shortcut F12
Guest UserPosted May 17, 2016, 8:37 AM
Thanks for your comments! I liked silver jubilee word :)
Bhuvanesh MohankumarPosted May 17, 2016, 8:12 AM
Sumit Jolly: Small suggestion, some beginners have issue to find Developer Tools, you can add the short cuts [F12], which may be helpful.
Bhuvanesh MohankumarPosted May 17, 2016, 8:10 AM
Great Silver Jubilee Article of the series, Nice one