By punkrockbuddyholly


2010-11-03 16:32:54 8 Comments

I was playing around in jsfiddle.net and I'm curious as to why this returns true?

if(0 < 5 < 3) {
    alert("True");
}

So does this:

if(0 < 5 < 2) {
    alert("True");
}

But this doesn't:

if(0 < 5 < 1) {
    alert("True");
}

Is this quirk ever useful?

14 comments

@SwiftNinjaPro 2019-09-20 20:39:30

try phrasing your results as Number()

if(Number(0) < Number(5) < Number(3)) {
    alert("True");
}

or try this:

if(Number(0) < Number(5) && Number(5) < Number(3)) {
    alert("True");
}

I googled this because I was getting (3 >= 20) //returning true and I guess javascript was trying to check 3 as a boolean because I was getting this value from the elm.getAttribute(); function which console.log(); was printing in String form.

@ndoty 2018-07-30 15:12:44

because 0 is less then 5 then that returns true, and by default true is anything including and can be evaluated to 1 which is still less than 3 which again returns true

@truease.com 2010-11-09 08:48:02

0 < 5 < 3 
==> ( ( 0 < 5 ) < 3 )
==> true < 3
==> 1 < 3
==> true

@Hippocrates 2012-10-29 16:01:23

In addition to python, CoffeeScript is another language that supports chained comparisons, thus 3 < x < 10 would be converted to (3 < x && x < 10) in vanilla JS

@Rajkamal Subramanian 2011-12-06 13:50:21

A boolean operand when operated over a math operator returns a number. to check this we do

true + 1  which gives you 2.

So 0 < 5, the returned boolean(true) operated with math operator(<) will return a number. So it boils to 1<3 which returns true

@PAUL Mansour 2010-11-27 01:30:12

The answer to the second part of the question, "is this quirk ever useful?" is perhaps no, as noted by a previous answer, if it is indeed a quirk of the language (Javascript) that true is cast to 1, but that the programmer does not in general view 1 and true (and 0 and false) as the same thing.

If however you have a mental model of 1 being true and 0 being false, then it leads to all sorts of nice boolean techniques that are extremely useful, powerful, and direct. For example, you could increment a counter directly with the result of A > 100, which would increment the counter if A is greater than 100. This technique might be viewed as a quirk or a trick in Java, but in an array or functional language may be idiomatic.

A classic example in the array language APL would be to count the number of items in an array that are (say) greater than 100:

+/A>100

Where if A is the 5 item array 107 22 256 110 3 then:

A>100

yields the 5 item boolean array:

1 0 1 1 0

and summing this boolean result:

+/1 0 1 1 0

yields the final answer:

3

This question is a perfect example of where this technique would be very useful, especially if the problem is generalized to determine if n out of m boolean values are true.

Check if at least two out of three booleans are true

@netrox 2010-11-04 05:06:43

That's easy.

(0 < 5 < 3)

Start with left to right so it evaluates the first 0 < 5. Is it true? Yes. Since TRUE=1, it evaluates 1 < 3. Since 1 is less than 3 so it's true.

Now with this

 (0 < 5 < 1)

Is 0 less than 5? Yes. So make it TRUE which also means 1. Now with that fact in mind, it evaluates to (1 < 1). Is 1 less than 1? No, therefore it's false. It has to be equal.

@Hippocrates 2010-11-03 20:01:56

I ran into this a little while ago in Obj-C and was very puzzled by it. I got the results I wanted by doing something like this:

if(0 < 5  && 5 < 3) {
alert("True");}

Which of course is false so you wouldn't get that "true" alert. Glad I read this, I now know why.

@Zach Johnson 2010-11-03 20:31:21

As to your question whether this quirk is ever useful: I suppose there could be some case where it would useful (if condensed code is what you are after), but relying on it will (most likely) severely reduce the understandability of your code.

It's kind of like using post/pre increment/decrement as a part of bigger expressions. Can you determine what this code's result is at a glance?

int x = 5;
int result = ++x + x++ + --x;

Note: with this code, you can sometimes even get different results depending on the language and compiler.

It's a good idea to make life easy for yourself and the next guy who will read your code. Clearly write out what you actually want to have happen rather then relying on side effects like the implicit conversion of booleans.

@punkrockbuddyholly 2010-11-05 14:12:18

Out of curiosity, is result 18?

@Zach Johnson 2010-11-05 19:27:10

@MrMisterMan: I'm not certain about Javascript, but in Java and C# the evaluation is guaranteed to be left to right, and the result is indeed 18. In some languages, such as C and C++, there is no guarantee it will be evaluated left to right, and you may end up with different results depending on the optimizations added by your compiler.

@Alan Geleynse 2010-11-03 16:34:58

Order of operations causes (0 < 5 < 3) to be interpreted in javascript as ((0 < 5) < 3) which produces (true < 3) and true is counted as 1, causing it to return true.

This is also why (0 < 5 < 1) returns false, (0 < 5) returns true, which is interpreted as 1, resulting in (1 < 1).

@rsenna 2010-11-03 16:37:57

And because JavaScript is NOT Python. :-)

@punkrockbuddyholly 2010-11-03 16:39:01

You answered while I was editing my question to add the if(0 < 5 < 1) == false. All is clear now, thanks :)

@Alan Geleynse 2010-11-03 16:39:07

Exactly, Python is the only language I know of that treats this syntax as ((0 < 5) && (5 < 3)), there are probably others but I don't know of them.

@Joren 2010-11-03 19:07:44

@Alan: Mathematica is another example.

@Vitor Tyburski 2014-04-11 18:09:26

And is neither Ruby... :P

@Michał Perłakowski 2016-01-09 21:33:34

IMHO JavaScript should raise TypeError when trying to compare boolean with number, because it makes no sense.

@David 2010-11-03 16:39:17

is it evaluating 0<5 which would return 1 for true when 1<3 which is true?

C# want let you do this "Operator '<' cannot be applied to operands of type 'bool' and 'int'"

@Arman McHitarian 2013-06-19 12:14:03

Sometimes I miss C#'s strictness in dynamic languages.

@Jack 2010-11-03 16:36:05

probably because true is assumed as 1 so

0 < 5 < 3  -->  true < 3 -->  1 < 3  --> true

@Harmen 2010-11-03 16:35:13

Because true < 3, because true == 1

@CaffGeek 2010-11-03 16:34:48

My guess is because 0 < 5 is true, and true < 3 gets cast to 1 < 3 which is true.

@erickson 2010-11-04 18:27:08

There's no casting here. A cast is an operator, which the programmer uses to explicitly check a type. This is implicit conversion from a boolean to an integer.

@CaffGeek 2010-11-04 18:49:35

@erickson, really...do we NEED to be hung up on semantics here?

@Mateen Ulhaq 2010-11-04 22:48:57

Don't worry about erickson. I misuses the word semantic too. :)

@Jack 2010-11-05 18:33:14

In any case the correct term is coercion. And yes, erickson is partially wrong with it's absolute certainty. A coercion is in any case a kind of cast also if usually (but it's just a convention) you use the word "cast" to express explicit type conversions. Type conversion == Type casting.

@Arman McHitarian 2013-06-19 12:11:38

Sophists all the way... The reply is 'laconically' nice anyway ;)

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

20 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

39 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

28 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

4 Answered Questions

[SOLVED] What does the ??!??! operator do in C?

  • 2011-10-19 16:56:59
  • Peter Olson
  • 261609 View
  • 2004 Score
  • 4 Answer
  • Tags:   c operators trigraphs

26 Answered Questions

[SOLVED] Can (a== 1 && a ==2 && a==3) ever evaluate to true?

11 Answered Questions

7 Answered Questions

[SOLVED] Why does Google prepend while(1); to their JSON responses?

13 Answered Questions

[SOLVED] event.preventDefault() vs. return false

9 Answered Questions

[SOLVED] Why does ++[[]][+[]]+[+[]] return the string "10"?

  • 2011-08-26 08:46:14
  • JohnJohnGa
  • 203442 View
  • 1675 Score
  • 9 Answer
  • Tags:   javascript syntax

Sponsored Content