+ 4
JS why this code increment a but not b ?
3 Réponses
+ 4
if (!(a++>=10 || b++>=11))
Short-circuit evaluation 🤔
Your condition combines 2 logical expressions .
It's combination of a++>=10 and b++>=11 .
If any of the both expressions is true in this statement the result of entire statement will be true.
When first operand is true logical OR will not evaluate second operand .
And here
a++>=10 is true so second expressions never gets evaluated! And hence no increment in `b`
take note that this happens in case of logical AND also.
if first operand is false second one won't be checked .
Read *Short-circuit evaluation*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
+ 2
When you use the || if the first condition is true it won't check the other condition since only one condition has to be true. So it checks "a" sees that the first condition is true and doesn't check b, hence why it doesn't increment.
+ 2
ok... thank you Odyel and 🇮🇳Omkar🕉 //Busy , Exams.