15

I would like to ask why

alert(3>2>1);  // (1)

Is returning FALSE in Javascript.

I know that the correct is:

alert(3>2 && 2>1); // (2)

But the code 1 should return either an error message or either TRUE! Is there a specific reason that this equation returns FALSE?

Wayne
  • 59,728
  • 15
  • 131
  • 126
George
  • 151
  • 1
  • 3

5 Answers5

40

If you add parentheses to show how JavaScript is interpreting it, it gets much clearer:

alert( (3 > 2) > 1 );

Let's pick this apart. First, it evaluates 3 > 2. Yes, three is greater than two. Therefore, you now have this:

alert( true > 1 );

true is coerced into a number. That number happens to be 1. 1 > 1 is obviously false. Therefore, the result is:

alert( false );
icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • Yeah guys I know what is happening. I am asking why.. Why didn't they fix that ??? – George May 01 '11 at 23:09
  • 14
    @George - ...because it's not broken. – Wayne May 01 '11 at 23:12
  • 2
    Fix what? It works as it should. – Brian Roach May 01 '11 at 23:14
  • 3
    @George - Also, for the love of god, one question mark per question, please. – Wayne May 01 '11 at 23:15
  • Yes, my question was why Python can return TRUE for print(1<2>1); BUT Javascript cannot do that. If it cannot do that. It should return an error. Not require the usage of parenthesis. OR at least show the result BUT inform user that this might not be the desired one! – George May 01 '11 at 23:21
  • 2
    @George: Python is different, there is not point in comparing these languages. You could also ask why Java, C, etc cannot do that. I agree that JavaScript *could* throw a syntax error here. But dynamic typing makes it possible ;) – Felix Kling May 01 '11 at 23:27
  • 1
    @George there is no syntactic or semantic problem with the expression. It's only "wrong" if you are attempting to impose the ways of another programming language on this one. – Pointy May 01 '11 at 23:27
  • Yea guys. I understand and I agree with everything that you said! But it should return an error. Not performing the equation wrongly. If in Maths 1<2>1 is TRUE. Then IN MY OPINION it is not acceptable for a programming language to return a wrong result. And the same thing is happening with C, C++, Matlab! – George May 01 '11 at 23:32
  • 1
    @George: It is simply that the **semantic** of such an expression in JavaScript is **different** from *your* interpretation. So why should it throw an error then? It has a defined meaning but it is not the one you are used to. Another example: In math you often use `^` to express exponentiation, e.g. `2^3`. In most languages this symbol is used for `XOR`. You might not be used to that, but should they all throw an error then if you use such an expression? – Felix Kling May 01 '11 at 23:35
  • 9
    @George the result **is not wrong** - it's exactly what the language specification says it should be. Either become the designer of the next big language, setting things "right" in yours, or accept that programming languages have their own rules for their own reasons. – Pointy May 01 '11 at 23:35
7

First 3>2 evaluates to TRUE, which is probably implicitly converted to 1, so you end up with 1>1, which is FALSE.

You might want an error here, but Javascript is very weakly typed, so it will try to do implicit conversions, without complaining.

EDIT:

So you're asking why the programming language syntax does not always coincide with the mathematical notation? I would say (1) they have different priorities and (2) it makes more sense for the compiler to do it another way.

This is not uncommon though:

  • "x = 3" (statement) and x = 3 (assignment)
  • "x >> 1" (much more than 1) and x >> 1 (bitshift)
  • "a | b" (a divides b) and a | b (bitwise OR).

The list goes on...

Gustav Larsson
  • 8,199
  • 3
  • 31
  • 51
4

It's being evaluated like:

+(3>2) > 1

Which is the same as:

+(true) > 1

Which finally is:

1 > 1 = false

But hey, at least 3 > 2 > 0 will work ;)

Matt
  • 43,482
  • 6
  • 101
  • 102
1

Lets break it down fundamentally, its two > operators with same precedence, So which one runs first?

JavaScript has Operator precedence assigned, As per Operator precedence table downloaded from MDN

Greater than(>) (11 in table) runs left to right, so 3>2 runs first which evaluates to TRUE

so now we have TRUE > 1,

when JavaScript sees two different types of values(bool and number here) for comparison, type coercion will happen which means TRUE will be coerced(type conversion) to 1,

So JavaScript will run as 1>1 which will result as FALSE

enter image description here

pk_code
  • 2,608
  • 1
  • 29
  • 33
0

Please find answer Below:

3 > 2 > 1 === true;

The > operator has a higher precedence than === and is left-to-right associative. If we add the implicit parentheses we get this:

((3 > 2) > 1) === true;

This evaluates further to:

((3 > 2) > 1) === true;
(true > 1) === true;
false === true;
false;
kiran malvi
  • 1,058
  • 10
  • 21