In Firefox 45 on OSX, when I fetch an item from localStorage
from a key that does not exist, the function call returns null
. I tested this in the console.
If I instead assign the call result to a variable, and print its value in the console, I get "null"
, i.e. a string.
Why does a variable assignment of a previously not defined variable cast a call result to a String?
Used code (in the console):
localStorage.getItem("non-existing-key"); // returns null
var x = localStorage.getItem("non-existing-key");
x // returns "null"
Edit: both versions seem to behave correctly on Chrome 50.0.2661.86 on OSX (both return null
)
Edit2: my mistake. I used another variable name in my tests (specifically: var name
). Now, if I let the console return the value of the variable name
, it returns window.name
, which is a property of window
of the type String, defaulting to "null"
. So, it's not an assignment that causes a cast, but instead its that I got a String property defined by window
.