Search code examples
javascriptlogicconditional-operatoradobe-indesignextendscript

What is the result of true && true && true && false && true && true && true in javascript?


Should the following:

true && true && true && false && true && true && true

(with 6 "trues" and 1 "false" in the middle, all connected by &&) result in true or false? The program I'm using, Adobe InDesign (which uses a JavaScript-based scripting language), gives true? Also how do you calculated the result of a conditional that consists of lots of "trues" and "falses"?


Solution

  • In a conforming JavaScript implementation, any sequence of the form (x1 [&& x2]*) is falsy if any expression xN is falsy — it evaluates to the first falsy xN value (or last xN if none are falsy), via leftward association of &&.

    Leftward association means (x1 && x2 && …) is equivalent to ((x1 && x2) && …), which can trivially be applied to the expression originally shown to determine that the “expected” result in JavaScript is false.

    This conjunction behavior is expected behavior in most programming languages.

    If “other operators” are mixed in, in “ways not shown”, it’s possible the premise in the question is not accurate due to precedence rules. Note that each xN above is unambiguous, by substitution. Likewise, if it’s not a conforming JavaScript engine…