【问题标题】:Can you use AND and OR in an if statement? [duplicate]你可以在 if 语句中使用 AND 和 OR 吗? [复制]
【发布时间】:2019-12-17 20:21:04
【问题描述】:

我想表达的逻辑是:

  • 如果condition_1 AND
  • condition2 或 condition3 AND
  • condition_4那么
  • console.log("THING ONE")
  • 否则
  • console.log("THING TWO");

编辑 - 在编写时发现原始问题在其他代码中,因此发布答案 Q&A-style

【问题讨论】:

  • 鼓励在 SO 上进行自助回答。但是,请确保这是一个以前没有被问过的问题。 if中有数百个关于使用多个条件的问题

标签: javascript


【解决方案1】:

是的,如果or 条件在括号中,看起来你可以,例如:

var logged_in = true;
var author_username = "fred";
var username = "fred";
var is_editor = false;
var is_something = true;


if (logged_in === true && (author_username === username || is_editor === true) && is_something === true) {
  console.log("THING ONE");
} else {
  console.log("THING TWO");
}

另见:

How to specify multiple conditions in an if statement in javascript

Which Logic Operator Takes Precedence

【讨论】:

    猜你喜欢
    • 2010-12-20
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2015-01-08
    • 2012-02-05
    • 1970-01-01
    相关资源
    最近更新 更多