javascript - boolean operators evaluation order -
i've piece of code , can't figure out execution order:
var enabled = item.get_property('quantity') > 0 && item.get_property('unitprice') > 0 || item.get_id() === null
mozilla states here &&
operator has higher precendence ||
, i'd expect that:
- is
item.get_property('quantity') > 0
true? if yes, move forward - is
item.get_property('unitprice') > 0
true? if yes, move forward - is
item.get_id() === null
true? if yes, return yes
but it's not way works. returns 'true' if item.get_id() === null
equals true.
if have expression x b y c, x , y being operators, x having higher precedence means expression equivalent ((a x b) y c). can see, if substitute && x , || y, intrepreter evaluate && b, meaning if false shortcut false, if true check boolean value of b. if (a && b) true, interpreter shortcut true, if not, check c , return true if evaluates true.
the following table summarizes possible scenarios:
a b c evaluates result 0 0 0 a,c 0 0 0 1 a,c 1 0 1 0 a,c 0 0 1 1 a,c 1 1 0 0 a,b,c 0 1 0 1 a,b,c 1 1 1 0 a,b 1 1 1 1 a,b 1
Comments
Post a Comment