5

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/

Steve B
  • 36,818
  • 21
  • 101
  • 174
  • 2
    Because it is not `null`. It is, instead, `Not a Number` – David Starkey Jun 28 '13 at 15:56
  • 3
    Have you tried using [isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FisNaN)? – Michael Todd Jun 28 '13 at 15:57

2 Answers2

11

You should use

isNaN(value);

to check for NaN

Because:

console.log(NaN === NaN); // false!

NaN is not equal to itself.

This may seem weird but it makes sense if you think about the nature of NaN.

Let's say you have this code:

var a, b, c;
// a = 0;oops a is still undefined so we'll get NaN if we do an operation with it
b = 5;
c = 6
if (a + b === a + c) {
    console.log("math error?");
}

You don't want to come to the seeming conclusion that 5 === 6.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • `NaN!==NaN`? That's just throwing basic logic out the window :D – David Starkey Jun 28 '13 at 15:58
  • @DavidStarkey A dog is not a number, and a banana is also not a number, but that doesn't mean `dog === banana`. – JJJ Jun 28 '13 at 16:00
  • Not a Number IS Not a Number. Saying Not a Number is not Not a Number is like saying Not a Number is a Number... Crazy ways to read code. It's like if you said Logic is based on the premise `A=A` but in this case `A!=A`, so all logic is now rendered unreliable. :) – David Starkey Jun 28 '13 at 16:01
  • @Juhana But what confuses me is that `NaN` is just a reference to a function, right? So why wouldn't the references be equal? Maybe I'm missing something obvious. I need to Google this... **EDIT:** Nevermind, I'm thinking about `isNaN`, not `NaN` – Ian Jun 28 '13 at 16:01
  • 1
    Thanks Frits van Campen, this solved my issue. I've learned a new thing today. I'll mark your answer as soon as I'm allowed to do. – Steve B Jun 28 '13 at 16:02
  • 1
    Math (and logic) has support for constructions like `NaN`. When you say 5 === 5 = true you're saying that 5 shares the same identity, `NaN` does not share an identity. – Halcyon Jun 28 '13 at 16:03
  • @Juhana Yeah nevermind, I realized that after I commented and edited my comment, sorry :) – Ian Jun 28 '13 at 16:03
  • 1
    @DavidStarkey This subject confuses me too; maybe this helps: http://stackoverflow.com/questions/10034149/why-is-nan-not-equal-to-nan – Ian Jun 28 '13 at 16:09
  • 1
    @DavidStarkey @Juhana It'd make more sense if if `NaN === NaN` evaluated to something representing "unknown", instead of actually evaluating to `false`. In that case, you could use http://en.wikipedia.org/wiki/Three-valued_logic Saying `+"dog" === +"banana"` might be a bit crazy, but certainly no more so than saying `+"dog" !== +"dog"` – Tim Goodman Jun 28 '13 at 16:17
  • 2
    @TimGoodman `something representing "unknown"`...like `undefined`? Haha – Ian Jun 28 '13 at 17:00
  • @Ian Yeah, but I didn't want to offer an opinion on whether it should be `undefined` or `null` or whatever, so I was intentionally vague. In JavaScript `undefined` is already used as the value of a variable that has never had a value assigned to it. If you have `var x; x = (NaN == NaN);` it isn't totally obvious to me that `x` should be in the same state after the second line as it is after the first line. – Tim Goodman Jun 28 '13 at 17:49
  • Really, where JavaScript uses `undefined` I would prefer it used "unassigned". Instead we get weirdness where accessing `x` gives an `x is not defined` error, unless you first say `var x;`, after which it is now defined... as `undefined`. ;) – Tim Goodman Jun 28 '13 at 17:51
  • @TimGoodman Oh no, I completely understand and agree...I was just being a pain :) – Ian Jun 28 '13 at 18:13
1
if(isNaN(candidate))return null; else return candidate;    
JJ123456
  • 11
  • 1