Introduction

HI. This article explains what the basic difference of HTML 5 local storage and session storage is in a practical way. I hope you will like it.
If you are new to the term local storage, I recommend you to read my article here.
A basic scenario where you can use
So let's start
Here is my application I have added two HTML5 pages.
In HtmlPage1.html I have set local storage and session storage, now I am trying to access that in HtmlPage2.html. Please see the following source.
HtmlPage1.html
  1. <!DOCTYPE html>
  2. <html
  3. xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Difference between local storage and session storage</title>
  6. <script>
  7. localStorage.setItem("myKey", 1);
  8. sessionStorage.setItem("myKey", 1);
  9. var myVal = localStorage.getItem("myKey");
  10. alert("My local storage value is : " + myVal);
  11. alert("My session storage value is : " + sessionStorage.getItem("myKey"));
  12. </script>
  13. </head>
  14. <body>
  15. </body>
  16. </html>
HtmlPage2.html
  1. <!DOCTYPE html>
  2. <html
  3. xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Difference between local storage and session storage</title>
  6. <script>
  7. var myVal = localStorage.getItem("myKey");
  8. alert("My local storage value is : " + myVal);
  9. alert("My session storage value is : " + sessionStorage.getItem("myKey"));
  10. </script>
  11. </head>
  12. <body>
  13. </body>
  14. </html>
When you run the first page, in the browser console you will see as in the following image.

Local Storage

Session Storage

Now it is time to run the second page.
Once you run that please check in the console. You can see as follows.
Local storage

Session storage

Please note that session storage is null. You can see the session storage only if you set it in that running page. I hope you like it.

Conclusion

Adding points as suggested by Afzaal Ahmed Zeeshan.
Session storage stays active until the session stays; the browser is active, a user is working, etc. Once the session is cleared they are cleared
Please provide your valuable suggestions and comments. Thanks in advance.
Kindest Regards,
Sibeesh Venu
Sibeesh Passion