Home  >  Article  >  Web Front-end  >  What are the three logical operators in javascript

What are the three logical operators in javascript

青灯夜游
青灯夜游Original
2021-12-08 16:17:063806browse

Three logical operators in js: 1. Logical AND operator "&&", which returns true only when both operands are true; 2. Logical OR operator "||", As long as one of the two operands is true, it will return true; 3. The logical NOT operator "!" is inverted. If the operand is not true, it will return true.

What are the three logical operators in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Logical operators are usually used to combine multiple expressions. The result of a logical operator is a Boolean value, which can only have two results, either true or false. The following table lists the logical operators supported in JavaScript:

Operator Name Example
&& Logical AND x && y means if both x and y are true, then it is true
|| Logical OR x || y means if either x or y is true, then it is true
! Logical not !x means if x is not true, then it is true
  • Logical AND operation (&&) is an AND Boolean operation. Returns true only if both operands are true, otherwise returns false.
  • Logical OR operation (||) is a Boolean OR operation. If both operands are true, or one of them is true, it returns true, otherwise it returns false.

  • The logical NOT operation (!) is a Boolean negation operation (NOT). As a unary operator, it is placed directly before the operand, converts the value of the operand to a Boolean value, then inverts and returns it.

Example:

var year = 2022;
// 闰年可以被 400 整除,也可以被 4 整除,但不能被 100 整除
if((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))){
    console.log(year + " 年是闰年。");
} else{
    console.log(year + " 年是平年。");
}

What are the three logical operators in javascript

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What are the three logical operators in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn