Does a javascript if statement with multiple conditions test all of them?

0 votes
asked Dec 18, 2009 by da

In javascript, when using an if statement with multiple conditions to test for, does javascript test them all regardless, or will it bail before testing them all if it's already false?

For example:

 a = 1
 b = 2
 c = 1

 if (a==1 && b==1 && c==1)

Will javascript test for all 3 of those conditions or, after seeing that b does not equal 1, and is therefore false, will it exit the statement?

I ask from a performance standpoint. If, for instance, I'm testing 3 complex jQuery selectors I'd rather not have jQuery traverse the DOM 3 times if it's obvious via the first one that it's going to return FALSE. (In which case it'd make more sense to nest 3 if statements).

ADDENDUM: More of a curiosity, what is the proper term for this? I notice that many of you use the term 'short circuit'. Also, do some languages do this and others dont?

9 Answers

0 votes
answered Dec 18, 2009 by anon

The && operator "short-circuits" - that is, if the left condition is false, it doesn't bother evaluating the right one.

Similarly, the || operator short-circuits if the left condition is true.

EDIT: Though, you shouldn't worry about performance until you've benchmarked and determined that it's a problem. Premature micro-optimization is the bane of maintainability.

0 votes
answered Dec 18, 2009 by annie

It exits after seeing that b does not equal one.

0 votes
answered Dec 18, 2009 by david-m

It short circuits - only a and b will be compared in your example.

0 votes
answered Dec 18, 2009 by albertein

It will only test all the conditions if the first ones are true, test it for yourself:

javascript: alert (false && alert("A") && false);
0 votes
answered Dec 18, 2009 by azazul

That's why you can do in javascript code like

var x = x || 2;

Which would mean that if x is undefined or otherwise 'false' then the default value is 2.

0 votes
answered Dec 18, 2009 by brad

From a performance standpoint, this is not a micro-optimization.

If we have 3 Boolean variables, a, b, c that is a micro-optimization.

If we call 3 functions that return Boolean variables, each function may take a long time, and not only is it important to know this short circuits, but in what order. For example:

if (takesSeconds() && takesMinutes())

is much better than

if (takesMinutes() && takesSeconds())

if both are equally likely to return false.

0 votes
answered Dec 7, 2012 by pazoozatest-pazman

Another reason why stopping evaluation with 1 or more parameters to the left.

if (response.authResponse && (response.authResponse.accessToken != user.accessToken)){ ... }

the second evaluation relies on the first being true and won't throw a compile error if response.authResponse is null or undefined etc because the first condition failed.

Other languages had this problem in the early days and I think it's a standard approach in building compilers now.

0 votes
answered Dec 21, 2015 by ivcandela

In case someone's wondering if there is a way to force the evaluation of all condition, there are the non short-circuit versions of && and || which are simply & and |

var testOr = true | alert(""); //alert pops up
var testAnd = false & alert(""); //alert pops up

the curious thing is that MDN docs don't talk about this.

Hope it helps someone who arrived here looking for information like this (like me)

0 votes
answered Sep 15, 2017 by brighteyed

For anyone on this question confused because they're not seeing the short-circuit behaviour when using an || in conjunction with an ? operator like so:

x = 1 || true ? 2 : 3 // value of x will be 2, rather than 1 as expected

it seems like the short circuit rule isn't working. Why is it evaluating the second term of the || (true ? 2 : 3) when the first is true? It turns out to be an order of operations problem because the above is the equivalent of

x = (1 || true) ? 2 : 3

with the || evaluated first and the ? evaluated second. What you likely want is:

x = 1 || (true ? 2 : 3)

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...