H
H
hbrmdc2015-09-26 21:55:57
JavaScript
hbrmdc, 2015-09-26 21:55:57

How to use localStorage correctly?

I learned that you can't store objects in localStorage, but you can do a hack using JSON.stringify:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };

// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));

Actually the question arose - why do you have to use a hack? it's not easy there is no way to save objects - maybe this is an outdated approach and there are better alternatives to localStorage?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question