Local storage allow developers to create persistent data within the browser by means of key value pairs. The benefit of this is that it allows the data to persist even after the browser window has closed.
One thing to note, the data is constrained to the local browser that the page has being loaded from. And that data is sandboxed to that specific domain. For example: https://yourSite.com would be different to http://yourSite.com
In the past cookies were used, however they were limited because of their 5kb file size. Local storage allows up to 5MB of data.
The api is a fairly simply one to use, it consists of four methods.
> localStorage.setItem('id', 'one');
> localStorage.setItem('age', 'mystery');
> localStorage.setItem('name', 'robert');
or
> localStorage.id = 'one'
which yields
> localStorage
Storage {age: "mystery", id: "one", name: "robert", length: 3}
> localStorage.getItem('id')
"one"
or
> localStorage.age
"mystery"
> localStorage.removeItem('age')
> localStorage
Storage {id: "one", name: "robert", length: 2}
> localStorage.clear();
> localStorage
Storage {length: 0}
> localStorage.key(1);
> 'id'
> localStorage.length;
> 3
var age = localStorage.age;
> age
"mystery"
var profile = {
'name': {
'first': Robert,
'last': James},
'age': 10
}
localStorage.setItem('id', JSON.stringify(profile));
> localStorage
Storage {profile: "{"name":{"first":"Robert","last":"James"},"age":"10"}", length: 1}
> var age = document.querySelector('age').value;
> localStorage.setItem('age', age);
Robert James @bkrjames