3 ms·
Almost! Except if you want to store objects/arrays/numbers (localStorage only supports strings), or want your site to work in Safari Private mode (where localSt
by marcuswestin 10y ago
Almost! Except if you want to store objects/arrays/numbers (localStorage only supports strings), or want your site to work in Safari Private mode (where localStorage breaks), or if you have to support legacy environments with old browsers (like many government services), etc etc etc :)
- diggan 10y agoNot that I disagree with your other arguments but this one sticks out: > Except if you want to store objects/arrays/numbers (localStorage only supports strings) My normal usage of localStorage usually looks like this: const getItem = (item) => { JSON.parse(window.localStorage.getItem(item)) } // and const setItem = (item, val) { window.localStorage.setItem(item, JSON.stringify(val)) } And then you won't have issues with any type.
- marcuswestin 10y agoExcept when a value has already been stored previously without JSON.stringify (in which case JSON.parse will throw), or you need a default value if unset (e.g `store.get('username', 'anonymous')`, or ... :) But I also see your point that sometimes the very quick and simple solution can be useful!