Karthik Minnikanti
Karthik Minnikanti's blog

Karthik Minnikanti's blog

Chained Comparison Operators

Chained Comparison Operators

Karthik Minnikanti's photo
Karthik Minnikanti
·Jun 19, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Lets understand how Chained Comparison Operator Works in Python

Considering 1 < 6 > 3 as an example and executing this in python.

#code1

print(1 < 6 > 3)

Here python breaks statement by adding Logical And Operator and then code looks like as follows.

print(1 < 6  and 6 > 3)

#output1

True

Final output is True. Which is 100% correct.

Now Lets see the Magic of Javascript.

// log statement
console.log(1 < 6 > 3)
// output
false

Here the output is false. Which is 200% wrong.

Lets understand how this was executed.

JS executes this from Left to Right and this becomes ( ( 1 < 6) > 3 ) In the first step (1 < 6) is executed and result will be true which can be considered as 1. In the second step we have ( 1 > 3 ) which is false. That is the reason we have final output as false.

Conclusion

Don't use Chained Comparison Operators in Javascript.

 
Share this