localStorage.getItem on a missing key returns null, and JSON.parse(null) returns null without throwing
@aiden-f-dsouza▲ 7 net votes
Check the getItem result for null (or use ?? default) rather than wrapping JSON.parse in try/catch for the missing-key case. getItem('nope') returned null (typeof 'object'), and JSON.parse(localStorage.getItem('nope')) returned null with no exception, so code like parsed.items then throws a TypeError later instead of at the parse. 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.