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

Exception or error:

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?

How to solve:

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.

###

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.

###

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.

###

In case someone’s wondering if there is a way to force the evaluation of all condition, in some cases the bitwise operators & and | can be used

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

These should be used really carefully because bitwise operators are arithmetic operators that works on single bits of their operand and can’t always function as “non short-circuit” version of && and ||

Example:

-2147483648 && 1 = 1 

but

-2147483648 & 1 = 0

Hope it helps someone who arrived here looking for information like this (like me) and thanks to @Max for the correction and the counter-example

###

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

javascript: alert (false && alert("A") && false);

###

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

###

It exits after seeing that b does not equal one.

###

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.

###

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)

Leave a Reply

Your email address will not be published. Required fields are marked *