-1

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"?

RobC
  • 22,977
  • 20
  • 73
  • 80
Vun-Hugh Vaw
  • 180
  • 1
  • 7
  • 3
    `InDesign` is broken according to JavaScript standards, but then that's why it's not JavaScript. – Pointy May 27 '20 at 02:46
  • Probably, but here is just a simplification, my original conditional is a lot jankier so it's probably tripping up InDesign. – Vun-Hugh Vaw May 27 '20 at 03:01
  • 3
    I can't say because I cannot *see* your "original conditional". – Pointy May 27 '20 at 03:06
  • @Pointy It's contextual only to InDesign and only to the script I'm writing. There's no way I can easily make sense of it here. – Vun-Hugh Vaw May 27 '20 at 03:39
  • Okay, this is purely a user error on my part. I've finally found the one typo that causes all this confusion. – Vun-Hugh Vaw May 27 '20 at 04:08
  • The following code using a ternary operator in InDesign alerts _false_ as expected: `true && true && true && false && true && true && true ? alert("true") : alert("false") ` – RobC May 27 '20 at 14:12

2 Answers2

4

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…

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
user2864740
  • 60,010
  • 15
  • 145
  • 220
1

it will be false

console.log(true && true && true && false && true && true && true)
Karthik n
  • 61
  • 2