具有多个条件的 javascript if 语句是否会测试所有条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1930499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:56:24  来源:igfitidea点击:

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

javascript

提问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?

在 javascript 中,当使用带有多个条件的 if 语句进行测试时,javascript 是否会测试它们,或者如果它已经是假的,它会在测试它们之前保释吗?

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?

javascript 是否会针对所有 3 个条件进行测试,或者在看到 b 不等于 1 并且因此为 false 之后,它会退出语句吗?

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).

我是从性能的角度来问的。例如,如果我正在测试 3 个复杂的 jQuery 选择器,我宁愿不让 jQuery 遍历 DOM 3 次,如果通过第一个选择器很明显它将返回 FALSE。(在这种情况下,嵌套 3 个 if 语句会更有意义)。

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?

附录:更令人好奇的是,这个的正确术语是什么?我注意到你们中的许多人使用“短路”这个词。另外,有些语言会这样做而其他语言不会吗?

回答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.

编辑:不过,在您进行基准测试并确定这是一个问题之前,您不应该担心性能。过早的微优化是可维护性的祸根。

回答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.

如果我们有 3 个布尔变量,a, b, c 就是一个微优化。

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:

如果我们调用 3 个返回布尔变量的函数,每个函数可能需要很长时间,不仅要知道这个短路,重要的是要知道它是什么顺序。例如:

if (takesSeconds() && takesMinutes())

is much better than

if (takesMinutes() && takesSeconds())

if both are equally likely to return false.

如果两者都同样有可能返回 false。

回答by azazul

That's why you can do in javascript code like

这就是为什么你可以在 javascript 代码中做这样的事情

var x = x || 2;

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

这意味着如果 x 未定义或为“假”,则默认值为 2。

回答by ivcandela

In case someone's wondering if there is a way to force the evaluationof all condition, in some casesthe 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 carefullybecause 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

希望它可以帮助到这里寻找这样的信息的人(像我一样)并感谢@Max 的更正和反例

回答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);

回答by David M

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

它短路 - 在您的示例中只会比较 a 和 b 。

回答by Annie

It exits after seeing that b does not equal one.

它在看到 b 不等于 1 后退出。

回答by PazoozaTest Pazman

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

使用左侧的 1 个或多个参数停止评估的另一个原因。

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

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.

第二个评估依赖于第一个为真,如果 response.authResponse 为空或未定义等,则不会抛出编译错误,因为第一个条件失败。

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

其他语言在早期就有这个问题,我认为这是现在构建编译器的标准方法。

回答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:

对于这个问题上的任何人都感到困惑,因为他们在将 an||?运算符结合使用时没有看到短路行为,如下所示:

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

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

似乎短路规则不起作用。||当第一项为真时,为什么要评估(true ? 2 : 3)的第二项?原来是一个操作顺序问题,因为上面的等价于

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

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

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

与被||评估的第一和被?评估的第二。你可能想要的是:

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

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