javascript 什么时候应该在 if 语句的条件中使用括号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22227248/
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-10-27 22:43:37  来源:igfitidea点击:

When should you use parentheses inside an if statement's condition?

javascriptjquery

提问by Dejo Dekic

I have a if condition like so:

我有一个 if 条件,如下所示:

$(document).ready(function(){
  var name = 'Stack';
  var lastname = 'Overflow';
   if( name == 'Stack' && lastname == 'Overflow' )
   alert('Hi Stacker!');
});

So my alertis fired...

所以我的alert被解雇了...

If I put my condition inside a brackets like this:

如果我把我的条件放在这样的括号内:

 $(document).ready(function(){
  var name = 'Stack';
  var lastname = 'Overflow';
   if( (name == 'Stack') && (lastname == 'Overflow') ){
      alert('Hi Stacker!');
   } 
});

My alertis also fired...

alert的也被开除了...

My question is: When and why I should use parentheses inside my if condition? Thank you!

我的问题是:何时以及为什么应该在 if 条件中使用括号?谢谢!

回答by zogby

There is no much difference in your example. You can read that for reference Operator Precedence (JavaScript)

你的例子没有太大区别。您可以阅读该内容以供参考 运算符优先级 (JavaScript)

回答by Peter

You use it to force associations, in your example this won't change anything. But consider this case:

您使用它来强制关联,在您的示例中,这不会改变任何内容。但请考虑这种情况:

A and B or C 

is a lot different from

A and (B or C)

Here you would be preventing the expression from being understood as (A and B) or Cwhich is the natural way javascript and maths do things.

在这里,您将阻止表达式被理解为(A and B) or Cjavascript 和数学做事的自然方式。

回答by mpm

Because of operator precedence :

由于运算符优先级:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

you'd better read this if you want more details

如果你想了解更多细节,你最好阅读这个

But basically == (equality) weights more than && (logical-and) , so A == Bis evaluated before C && Dgiven

但基本上 == (equality) 权重大于 && (logical-and) ,因此A == BC && D给出之前进行评估

C <- A == B

and

D <- E == F

so adding parenthesis to C or D dont matter,in that specific situation.

因此,在特定情况下,在 C 或 D 中添加括号无关紧要。

回答by Sunny

Brackets can be used, if multiple conditions needs to be checked.

如果需要检查多个条件,可以使用括号。

For ex: User is also a Stacker if full name is 'StackOverflow' then add another condition with 'Or' operator.

例如:如果全名是“StackOverflow”,则用户也是 Stacker,然后使用“Or”运算符添加另一个条件。

if(((name == 'Stack') && (lastname == 'Overflow')) || FullName =='StackOverflow')

As you can see, name and last name are placed inside one bracket meaning that it gives a result either true or false and then it does OR operation with FullName condition.

如您所见,姓名和姓氏放在一个括号内,这意味着它给出的结果是真或假,然后它与 FullName 条件进行 OR 运算。

And having brackets around name, lastname and fullname fields are optional since it doesn't make any difference to the condition. But if you are checking other condition with FullName then group them into bracket.

姓名、姓氏和全名字段周围的括号是可选的,因为它对条件没有任何影响。但是,如果您使用 FullName 检查其他条件,则将它们分组到括号中。

回答by MrPk

Brackets are never required in such situations. Of course, try to read first solution (without) and second solution (with). Second one it's clear, fast-readable and easy to mantain.

在这种情况下永远不需要括号。当然,尝试阅读第一个解决方案(无)和第二个解决方案(有)。第二个是清晰、快速可读且易于维护。

Of course if you have to change precedence (in this case you just have an AND condition, but what if you need and AND and an OR? 1 and 2 or 3 priority changes between "(1 and 2) or 3" - "1 and (2 or 3)"

当然,如果您必须更改优先级(在这种情况下,您只有一个 AND 条件,但是如果您需要 and AND 和一个 OR 呢?1 和 2 或 3 优先级在“(1 和 2)或 3”-“1”之间变化和(2 或 3)”

回答by Arjit

Brackets ()are used to group statements.

括号()用于对语句进行分组。

Ex - if you have an expression '2 + 3 * 5'.

Ex - 如果你有一个表达式'2 + 3 * 5'

There are two ways of reading this: (2+3)*5or 2+(3*5).

有两种阅读方式:(2+3)*52+(3*5)

To get the correct o/p based on operator precedence, you have to group the correct expression like *has higher precedence over +, so the correct one will be 2+(3*5).

要根据运算符优先级获得正确的 o/p,您必须将正确的表达式分组,例如*优先级高于+,因此正确的表达式将是2+(3*5)

回答by Smitt

Consider the statement

考虑语句

if( i==10 || j == 11 && k == 12 || l == 13)

what you would want is if either i is 10 or j is 11 And either k is 12 or l is 13 then the result shouldbe true, but say if i is 10, j is 11, k is 10 and l is 13 the condition will fail, because the fate of equation is decided at k as aoon as && comes in picture. Now if you dont want this to happen the put it like this

你想要的是,如果 i 是 10 或 j 是 11 并且 k 是 12 或 l 是 13 那么结果应该是真的,但是如果 i 是 10,j 是 11,k 是 10 并且 l 是 13,条件将失败,因为等式的命运是在 k 决定的,因为&&出现了。现在,如果你不希望这种情况发生,就把它像这样

if( (i==10 || j == 11) && (k == 12 || l == 13))

In this ORs will be executed first and the result will be true.

在此 OR 将首先执行,结果将为真。

回答by java seeker

first bracket requires for complex condition to ensure operator precedence correctly. lets say a example

第一个括号需要复杂的条件,以确保运算符优先级正确。让我们说一个例子

   var x=1,y=1,z=0;

    if(x==0 && y==1 || z==0)   
    {
      //always true for any value of x
    }
    what will happen here  
    (0 && 1 ||1) ----> (0 ||1)----->1 
    && has high precedence over ||

    if(x==0 && (y==1 || z==0)) alert('1');
    {
     //correct way
    }

if you do not use (y==1 || z==0)bracket the condition always will be true for any value of x.

but if you use (..) the condition return correct result.

如果不使用 (y==1 || z==0)括号,则条件对于 x 的任何值始终为真。

但是如果你使用 (..) 条件返回正确的结果。