Last Updated: February 25, 2016
·
823
· piatra

Trick with localStorage

I've been using this trick more an more lately to store data permanently in the browser.

data = JSON.parse(localStorage.getItem('data')) || [];
data.push({
  name: 'Andrei',
  age: 20
});
localStorage.setItem('data', JSON.stringify(data));

I've found it to be very useful. It's localStorage so it's on mobile too!
Support (http://caniuse.com/#feat=namevalue-storage)
* android 2.1
* IE 8+
* everyone else

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside