Home > Article > Web Front-end > Why Does \"(0 < 5 < 3)\" Evaluate to True in JavaScript?
Unveiling the Truth Behind "(0 < 5 < 3)"
In the realm of programming, the order of precedence governs how expressions are evaluated. In the case of "(0 < 5 < 3)", the evaluation follows a specific order, leading to an unexpected result of "true".
Delving deeper into the mechanics:
Therefore, "(0 < 5 < 3)" is interpreted as "((0 < 5) < 3)".
Evaluating the Nested Expressions:
Since true is interpreted as 1 in JavaScript, (true < 3) evaluates to (1 < 3), resulting in "true". Hence, the entire expression "(0 < 5 < 3)" returns "true".
An Interesting Quirk:
This peculiar behavior can be exploited to achieve certain outcomes. For instance, it can be helpful in executing logical operations conditionally.
Example:
<code class="javascript">let result = (condition) ? (0 < 5 < 3) : (5 < 3 < 0);</code>
If condition is true, the result will be "true" because the expression "(0 < 5 < 3)" evaluates to "true". Otherwise, the result will be "false" because the expression "(5 < 3 < 0)" evaluates to "false".
The above is the detailed content of Why Does \"(0 < 5 < 3)\" Evaluate to True in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!