-2

Possible Duplicate:
Why does (0 < 5 <3) return true?

How come this is true?:

console.log(100 < 210 < 200); // outputs true

Community
  • 1
  • 1
Ner
  • 1
  • see: [http://meta.stackexchange.com/questions/92074/what-can-i-do-when-getting-it-does-not-meet-our-quality-standards] – 4b0 Aug 21 '11 at 08:56

2 Answers2

3

That's equivalent to:

console.log((100 < 210) < 200);

which is equivalent to:

console.log(true < 200);

And this evaluates to true because when using operators like <, true is treated as if it were a 1

Consequently, the following will evaluate to false:

console.log(true < 0)
Drekembe
  • 2,620
  • 2
  • 16
  • 13
2

100 < 210 < 200 is equivalent to (100 < 210) < 200 which is true < 200 which is 1 < 200 which is true.

That last bit (true becoming 1) may be a bit surprising. It's the result of how JavaScript does relational operations (Section 11.8.5 of the spec), which says amongst other things that if the relation is between a non-number (other than null or undefined) and a number, you convert the non-number to a number, and the conversion of true to a number results in 1 (Section 9.3 of the spec.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875