Graphics Cove

Why Does LocalStorage getItem log [object Object]?

When adding items to local storage you might accidentally pass some information that gets stored as [object Object]. When you try and retrieve the information you added with localStorage.getItem, all you will see is[object Object].

There's a quick way to fix this. Local storage only supports the string datatype, therefore you will need to convert any data to a string before saving it to local storage with JSON.stringify().

Let's say you have a data object. You can add it to local storage like so:

localStorage.setItem('data', JSON.stringify(data))

When you want to get back your data object, simply convert it back to a JS object when reading from local storage:

data = JSON.parse(localStorage.getItem('data'))

© 2010 - 2023 Graphicscove

TwitterFacebookFAQsPrivacy PolicyFree Website Review