1

My question is quite simple.

I'm saving the data to local storage as string with JSON.stringify(data_object) and using JSON.parse(loaded_data) after load it back.

What is the best way to keep data on local storage? Save it in string format or as object? There are some difference is these approaches?

Thank you.

1
  • 2
    localStorage only stores strings. You have to JSON encode/decode an object to store it in localStorage. Commented Aug 17, 2017 at 19:49

1 Answer 1

2

You can only items in localStorage as strings:

localStorage.setItem('testObject', {test: 'object'});
console.dir(localStorage.getItem('testObject'));

You will see "[object Object]" because that is the string representation of the data you tried to store.

So your current method is correct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.