What is localStorage?

local storage is property for accessing Storage object, which is used to store and retrieve data from user’s browser. It is accessible only at client side not at server side like cookie.

Data stored in localStorage is accessible thought all pages under same domain, until user does not delete it manually. Even though user closes the browser, data will be accessible next time.

Syntax:

localStrorage stores data in Key/Value format. Same syntax can be written as

Note: Here ProductName is key and Mobile is value for local storage. Key is case sensitive so here ProductName and productname will be considered as two different keys.

Get local storage:

OR

localStorage will always return value in string. So if required, then one need to cast the value in needed type.

Remove local storage:

This will remove ‘ProductName’ from local storage of under current domain.

This will remove all the local storage for current domain.

What is sessionStorage?

Session storage is almost same as local storage. Only difference is, session storage will get cleared once user will close the browser window.

Syntax:

All the example written for localStorage can be used for sessionStorage as below

Set session storage:

OR

Get session storage:

OR

Remove session storage:

Browser Support for Local Storage and Session Storage:

Browser Version
Chrome 4.0
Firefox 3.5
Internet Explorer 8.0
Opera 10.50
Safari 4.0

Storage Capacity:

Storage capacity for localStorage and sessionStorage is vary based on browser. Generally browsers provides around 5MB of storage space.

Yatendrasinh Joddha
I am a software engineer currently living in Gandhinagar, India. My interest is in web and software development. I am also interested in sharing my knowledge with people.

I have expertise in asp.net and related Microsoft technologies, with JavaScript frameworks and libraries like JQuery, KnockOut.js,and AngularJS.
Yatendrasinh Joddha on FacebookYatendrasinh Joddha on GithubYatendrasinh Joddha on LinkedinYatendrasinh Joddha on TwitterYatendrasinh Joddha on Wordpress