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

Javascript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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

Solution 5 - Javascript

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

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

Solution 6 - Javascript

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

Solution 7 - Javascript

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.

Solution 8 - Javascript

It exits after seeing that b does not equal one.

Solution 9 - Javascript

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)

Solution 10 - Javascript

For this case:

a = 1
b = 2
c = 1

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

You can use:

if ([a, b, c].every(x => x == 1))
    // do something if a, b and c are equal to 1.

If you want to know if at least one is equal to 1, use the some() method instead of every():

if ([a, b, c].some(x => x == 1))
    // do something if a, b or c is equal to 1.

every() and some() are array methods.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDA.View Question on Stackoverflow
Solution 1 - JavascriptAnon.View Answer on Stackoverflow
Solution 2 - JavascriptBradView Answer on Stackoverflow
Solution 3 - JavascriptazazulView Answer on Stackoverflow
Solution 4 - JavascriptivcandelaView Answer on Stackoverflow
Solution 5 - JavascriptalberteinView Answer on Stackoverflow
Solution 6 - JavascriptDavid MView Answer on Stackoverflow
Solution 7 - JavascriptPazoozaTest PazmanView Answer on Stackoverflow
Solution 8 - JavascriptAnnieView Answer on Stackoverflow
Solution 9 - JavascriptBrightEyedView Answer on Stackoverflow
Solution 10 - JavascriptArturo ChenView Answer on Stackoverflow