Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 3 > 2 > 1 return false while 1 < 2 < 3 returns true? [duplicate]

Why does 3>2>1 return false while 1 < 2 < 3 returns true?

console.log(1 < 2 < 3);
console.log(3 > 2 > 1);
like image 391
Ramesh Rajendran Avatar asked Aug 02 '18 07:08

Ramesh Rajendran


People also ask

Why does true == true return false?

Because they don't represent equally convertible types/values. The conversion used by == is much more complex than a simple toBoolean conversion used by if ('true') . So given this code true == 'true' , it finds this: "If Type(x) is Boolean , return the result of the comparison ToNumber(x) == y ."

What does returning false do?

It simply stops the execution of the function(). That means, it will end the whole execution of process.

What is the difference between return true and return false in Java?

Using return causes your code to short-circuit and stop executing immediately. The first return statement immediately stops execution of our function and causes our function to return true . The code on line three: return false; is never executed.

How do I return a console log?

Hence, the return statement is passing the value x*x back to the call of square . If you want to view the value in ans, you would write: console. log(ans); or to simplify the problem as a whole you could write: console. log(square(5)); and my guess is you have this last line in your code which is why you are confused.


2 Answers

Since 1 < 2 evaluates to true and 3 > 2 evaluates to true, you're basically doing :

console.log(true < 3);
console.log(true > 1);

And true is converted to 1, hence the results.

like image 147
Zenoo Avatar answered Oct 12 '22 16:10

Zenoo


Because it interprets left to right and because it tries to cast to same type.

1 < 2 < 3 becomes true < 3, which because we are comparing numbers is cast to 1 < 3 which is truth.

3 > 2 > 1 becomes true > 1, which because we are comparing numbers is cast to 1 > 1 which is false.

like image 27
Emil S. Jørgensen Avatar answered Oct 12 '22 17:10

Emil S. Jørgensen