Select to view content in your preferred language

Does the OR operator in Arcade test short-circuit evaluation as it does in javascript?

734
3
Jump to solution
01-04-2024 03:49 AM
JamesNightingale
New Contributor

In Javascript the logical OR expression is evaluated left to right, it is tested for possible "short-circuit" evaluation using the following rule:

(some truthy expression) || expr is short-circuit evaluated to the truthy expression.

Short circuit means that the expr part above is not evaluated, hence any side effects of doing so do not take effect (e.g., if expr is a function call, the calling never takes place). This happens because the value of the operator is already determined after the evaluation of the first operand.

Does Arcade behave in the same way?

Tags (1)
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

It turns out my code was incorrect, since the function calls need parenthesis behind them. When I fixed it, it does short-circuit.

If the first function is false, then the second function runs

console1.png

If the first function is true, the second function doesn't run

console2.png

View solution in original post

3 Replies
KenBuja
MVP Esteemed Contributor

It doesn't look like you can have an expression that is a function call, even if it does return a boolean. For example, running this code gives the error "Test execution error: Execution error - Logical expression can only be combined with || or &&. Verify test data."

function first() {
  console('First');
  return true;
}

function second() {
  console('Second');
  return true;
}

return first||second;

 

0 Kudos
JamesNightingale
New Contributor

Hi KenBuja, 

Thanks for your response. Although that is interesting, it's not what I'm trying to find out - what I want to know is whether the Arcade OR operator tests short-circuit evaluation of the two expressions. Ignore the example of the function calls and instead replace them with:

If ((some truthy expression) || (some other expression) {

}

Would the second 'some other expression' get evaluated? In JS or C# it doesn't since the || operator short-circuits due to the first expression being truthy and therefore the second expression doesn't need to be evaluated.

 

0 Kudos
KenBuja
MVP Esteemed Contributor

It turns out my code was incorrect, since the function calls need parenthesis behind them. When I fixed it, it does short-circuit.

If the first function is false, then the second function runs

console1.png

If the first function is true, the second function doesn't run

console2.png