最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Both AND and OR in IF Statement - Stack Overflow

programmeradmin2浏览0评论

I'm working on a Google Spreadsheet adding scripts (Not formulas) and I am stuck on a problem.

I need to find a way to use something like this:

If (Product == "Shampoo" && (Box == "15" OR Box == "17"))
{
  //Do Something...
}

Basically IF the product is a shampoo and the box it belongs to is either 15 or 17 then do something. I know doing a If (Product == "Shampoo" && Box == "15" OR Box == "17") will produce unexpected or bad results. How do we go about using an OR with AND in google scripting?

From what I understand Google Scripts are based on Javascript but I can't seem to find help posts online or here in SO, all I get are solutions in formula not script.

I'm working on a Google Spreadsheet adding scripts (Not formulas) and I am stuck on a problem.

I need to find a way to use something like this:

If (Product == "Shampoo" && (Box == "15" OR Box == "17"))
{
  //Do Something...
}

Basically IF the product is a shampoo and the box it belongs to is either 15 or 17 then do something. I know doing a If (Product == "Shampoo" && Box == "15" OR Box == "17") will produce unexpected or bad results. How do we go about using an OR with AND in google scripting?

From what I understand Google Scripts are based on Javascript but I can't seem to find help posts online or here in SO, all I get are solutions in formula not script.

Share Improve this question edited Jan 9, 2018 at 18:32 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Jan 9, 2018 at 14:47 JayJay 6666 gold badges16 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

First a note about the OP code

JavaScript methods are case sensitive.

Instead of IF the correct syntax is if. By the other hand the Logical OR operator is ||.

Considering the above, the OP code could be replaced by

if(Product == "Shampoo" && (Box == "15" || Box == "17"))
{
  //Do Something...
}

and

if(Product == "Shampoo" && Box == "15" || Box == "17")

Regarding the question, if (Product == "Shampoo" && Box == "15" || Box == "17") returns unexpected or bad results, this could be hard to read and lead to confusions for humans but JavaScript have very specific rules regarding how operations should be made by the engine, in this case Rhino which is used by Google Apps Script.

As was mentioned in a previous answer in this case the rule is called operator precedence but in order to make the code easier to read and to prevent confusions a good practice is to enclose each parison in parenthesis.

Considering that the Logical AND has a higher precedence than Logical OR

(Product == "Shampoo" && Box == "15" || Box == "17")

is the same as

((Product == "Shampoo" && Box == "15") || Box == "17")

References

  • if..else
  • Logical OR

What are you looking for is the operator precedence of the two operators logical AND && and logical OR ||.

Part of the table:

Precedence  Operator type  Associativity    Individual operators
----------  -------------  ---------------  --------------------
       6    Logical AND    left-to-right    … && …
       5    Logical OR     left-to-right    … || …

You see a higher operator precendece of logical AND over logical OR. That means you need some parenthesis for the OR statement.

发布评论

评论列表(0)

  1. 暂无评论