您可以在if语句中使用AND和OR吗?

时间:2019-08-10 09:54:21

标签: javascript

我要表达的逻辑是:

  • 如果condition_1
  • condition2condition3
  • condition_4然后
  • console.log("THING ONE")
  • 其他
  • console.log("THING TWO");

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

1 个答案:

答案 0 :(得分:-2)

是的,如果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