Let's run this javascript code:
var value = parseInt("");
console.log(value != Number.NaN? value : null);
Why does this code outputs Nan
in the console instead of null
?
How can I change my code to actually get a null
object ?
I try to wrap my 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));
But this behaves the same.
Here is a jsFiddle that illustrate my issue: http://jsfiddle.net/stevebeauge/BRP94/