Assign null to javascript var - ends as String
1 answer
You are probably using this in the global scope, in which case it name
refers to a property Window.name
. Assigning a value to this property automatically converts the value to a string, for example try opening the browser console and enter the following:
var name = 123;
alert(typeof name);
You will most likely get a warning that reads string
.
However, if you put this in an IIFE (and make sure you have a declaration var
), it behaves as expected:
(function() {
var name = null;
if(name == null) {
alert("Name = null"); // shows alert
}
})();
+8
source to share