Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

While learning more about IE’s documentMode property, I came across some sniffing code, which, of cause, fails in browsers like Firefox.

However, a simplified version of the test would readas such:

if(undefined < 9) planA();
else planB();

Now in Firefox, it fall through to planB(), but so does this:

if(undefined > 9) planA(); // note: greater than
else planB();

The question is, is this the documented behaviour? That is, does the undefined value short-circuit the test?

share|improve this question
    
What problem are you really trying to solve? –  jfriend00 Jul 12 at 3:06
    
undefined is neither smaller nor greater than 9. Why would it be? –  Kilian Foth Jul 12 at 10:43

1 Answer 1

I’ve got it. The expression:

if(undefined > 9)

typecasts undefined as a number: in this case it is NaN.

NaN is the only value in JavaScript which doesn’t equal anything, including itself. As a result it is neither less than nor greater than another number, so the test is always false.

So, it’s not the undefined value as such, but its conversion it NaN which produces the result.

Thanks for the idea, Killian.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.