Firefox and Chrome return different values when accessing local storage via a key.
alert(localStorage.mykey); // FF = null, Chrome = undefined
alert(localStorage.getItem('mykey')); // FF = null, Chrome = null
See Firefox bug localStorage/sessionStorage should return undefined (not null) for undefined keys.
Essentially, Chrome and other browsers treat localStorage.mykey like any other array object where Firefox treats it like getItem().
So, my question is: Given this discrepancy, what is the best way to check if a key exists in localStorage?
Option 1: if(localStorage.getItem('key') === null)
Option 2: if(localStorage.key === null || localStorage.key === undefined)
Option 3: Any way to check localStorage.key without the || in #2?
Which version of Firefox were you using? Recent versions (I tested using Firefox 14) return
undefinedin this case. Anyway, the easy test istypeof localStorage.key == "string".