Consider this JavaScript snippet:
var value = parseInt("");
console.log(value != Number.NaN ? value : null);
Why does the console output Nan
instead of null
?
Is there a way to modify the code so that it actually returns a null
value?
I attempted to wrap the code in a function like this:
function parseIntOrDefault(value, def){
var candidate = parseInt(value);
if(candidate != Number.NaN) return candidate; else return def;
}
console.log(parseIntOrDefault('', null));
However, the outcome remains unchanged.
For further context and demonstration of the issue, refer to this jsFiddle: http://jsfiddle.net/stevebeauge/BRP94/