What is localStorage or sessionStorage in HTML5

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time until unless user deletes his cache, data stored in sessionStorage gets cleared when the originating window or tab get closed.

These are new HTML5 objects and provide these methods to deal with it:

The following snippet accesses the current domain’s local Storage object and adds a data item to it using Storage.setItem().

localStorage.setItem('myFav', 'Taylor Swift');

//or you can use the keyname directly as :

localStorage.myFav = 'Taylor Swift';</code>

To grab the value set in localStorage or sessionStorage, we can use


There’s no append function for localStorage or sessionStorage objects. It’s not hard to write one though.The simplest solution goes here:

// set the data
localStorage.setItem("key", "oldData"); 

// append some more data to it
localStorage.setItem("key", localStorage.getItem("key") + "newData");

But we can keep things modular and separate this as a function so to managed easily as below:

// This is basic function that get the stored data from <span style="color: #00ccff;">localStorage</span> by key-name and append newer into that
function appendToStorage(key, data){
    // get already stored data from localStorage
    var old = localStorage.getItem(key);
    //If nothing is there set it as blank
    if(old === null) old = "";
    // now append the data to localStorage
    localStorage.setItem(key, old + data);

// And it can be used as below
appendToStorage('key', 'newData');

Note: It makes more sense to define a function append on the localStorage object. However, because localStorage has a setter, this is not possible. If you were trying to define a function using localStorage.append = …, the localStorage object will interpret this attempt as “Save an object called append in the Storage object”.

If you are still confused about When to use localStorage v/s sessionStorage, here is a small comparision:

Storage Type Max Size Persistence Availability to other Windows/tabs Data Type Supported
LocalStorage 5MB per app per browser. According to the HTML5 spec, this limit can be increased by the user when needed; however, only a few browsers support this On disk until deleted by user (delete cache) or by the app Shared across every window and tab of one browser running same web app String only, as key-value pairs
SessionStorage Limited only by system memory Survives only as long as its originating window or tab Accessible only within the window or tab that created it String only, as key-value pairs

Say something : I accept all the "Humer&Critic"

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s