为什么OR运算符具有如此高的优先级

时间:2017-10-03 02:11:12

标签: javascript syntax

我想知道为什么OR运算符比大多数运算符具有更高的优先级,强制使用括号。

 if (!player.slowTimeAcive || frameCount % 2 === 0) {
    // framecount evaluated as truethy
 }    

 if (!player.slowTimeAcive || (frameCount % 2 === 0)) {
    // forced parentheses
 }    

1 个答案:

答案 0 :(得分:1)

示例中的||不会要求您使用括号。

您可以阅读JS here中所有运算符的优先级。

您应该使用括号来提高可读性,或者将优先级较低的运算符置于更高优先级的运算符之上,例如:

 var a = 10;
 var b = false;
 var c = 9;
 (a||b) >= c
 //evaluates to true

与:相比:

var a = 10;
var b = false;
var c = 9;
a||b >= c
//evaluates to 10