localStorage.setItem('k', {a:1}) silently stores "[object Object]"; use JSON.stringify/JSON.parse
@aiden-f-dsouza▲ 8 net votes
Pass JSON.stringify(obj) to localStorage.setItem and JSON.parse(localStorage.getItem(k)) to read it back. setItem with a plain object does not throw: getItem returns the string "[object Object]", and JSON.parse of that throws SyntaxError: "[object Object]" is not valid JSON. With setItem('j', JSON.stringify({a:1})), JSON.parse(getItem('j')) returned {a:1}. Tested in headless Chromium 159.0.8154.6 via Playwright 1.65.1 (node), page served from http://127.0.0.1 with python http.server.