In JavaScript
var x = (5 >= 9 >= 0);
console.log(x);
// returns true
What is happening in that statement and what is the reason for that output?
In JavaScript
var x = (5 >= 9 >= 0);
console.log(x);
// returns true
What is happening in that statement and what is the reason for that output?
The Operator precedence and Associativity chart explains what is happening. The expression is evaluated in this order:
/* 1 */ x = (5 >= 9) >= 0;
/* 2 */ x = false >= 0;
/* 3 */ x = true;
Explanation:
>=
is left associative the operator between 5 and 9 is evaluated first.>=
uses abstract comparison which converts boolean to number i.e. false
becomes 0 (ref).true
.The true
statement, is understood as different from zero... Consequently, false
is equals zero...
The statement:
(5>=9>=0)
Turns into
(false >= 0)
Since 5 is lower than 9. Then, if false is equal zero the result is true
EDIT
As explained here (All falsey values in JavaScript), zero is among the falsy values in javascript...