开发者

JavaScript; parsing an IF statement with OR clauses

开发者 https://www.devze.com 2023-02-09 14:33 出处:网络
My question is qu开发者_StackOverflow中文版ite simple, if I declare an IF statement with a series of OR clauses will JavaScript read all of the ORs or stop at the first one that is satisfied?

My question is qu开发者_StackOverflow中文版ite simple, if I declare an IF statement with a series of OR clauses will JavaScript read all of the ORs or stop at the first one that is satisfied?

Thanks in advance.


Stops at the first one. It's called short-circuiting

http://en.wikipedia.org/wiki/Short-circuit_evaluation https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators


It should only process upto the first OR that returns true:

if (a || b || c) { 

}

If a is false, b is true and c is true, it will process upto b.


function foo() {
    return true;
}

function bar() {
    alert("bar");
}

foo() || bar(); // true - no alert
bar() || foo(); // true - alert of "bar"


If the first condition is satisfied, other conditions of or are not evaluated

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号