Why does Javascript cast null to string at variable assignment? -
this question has answer here:
in firefox 45 on osx, when fetch item localstorage
key not exist, function call returns null
. tested in console.
if instead assign call result variable, , print value in console, "null"
, i.e. string.
why variable assignment of not defined variable cast call result string?
used code (in console):
localstorage.getitem("non-existing-key"); // returns null var x = localstorage.getitem("non-existing-key"); x // returns "null"
edit: both versions seem behave correctly on chrome 50.0.2661.86 on osx (both return null
)
edit2: mistake. used variable name in tests (specifically: var name
). now, if let console return value of variable name
, returns window.name
, property of window
of type string, defaulting "null"
. so, it's not assignment causes cast, instead got string property defined window
.
i made mistake. specific code used following:
var name = localstorage.getitem("non-existing-key"); name
now, getitem
return null
, not string. happens letting console print value of name
in fact window.name
(see window.name on mdn), default "null"
(a string).
Comments
Post a Comment