Home >Web Front-end >Front-end Q&A >What are the three logical operators in javascript
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.
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 |
&&
) 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 + " 年是平年。"); }
[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!