-1

I have this code

if (!caught.includes("AreYouHuman") && !caught.includes("pfail=1") && caught.includes("Episode") && caught.includes("Anime")) {
      var arraystring = caught.split("&s");
      var updating = chrome.tabs.update({url: arraystring[0] + "&s=beta&pfail=1"});
}

"caught" is a URL which I get from the browser.

1)would JavaScript stop at the first false statement it counters ( assuming that it iterates over them linearly ( AreYouHuman --> pfail=1 --> Episode -->Anime)).

2)or does it evaluate all of the statements at once then decide whether the whole thing is true or false ?

Assuming that 1) is the correct option, placing the least likely condition to be true,on the first position, would theoretically increase my code execution speed right ?

About the "duplicate" flag non-veteran programmers (like me) won't know "Short-circuit” and probably are not looking for the difference between "&" and "&&", there search would be how does javascript evaluate booleans in an "if statement"

OmG3r
  • 1,658
  • 13
  • 25

2 Answers2

0

Because you are using the short-circuit logical AND operator &&, yes, the entire expression will return false when the first of the expressions fails.

If, however, you were using a single &, then it would be a bitwise operator not be short-circuited and would continue to test the remaining conditions even if one were to return false.

The conditions are tested according to order of operations, but barring any grouping operators, the expression is evaluated left to right.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

Yes, javascript interpreter stops at first false statement, from left to right. If a false value is encountered, it stops the tests, as the expression will evaluates to FALSE whatever the other values are

Pierre
  • 643
  • 1
  • 7
  • 14