1.5.1 Logical Operators Overview
Concept: logical operators are used to perform Boolean operations on values, the return value is a Boolean value. Often developed later for determination of a plurality of conditions.
1.5.2 Logical AND &&
Returns true on both sides are true, otherwise false.
1.5.3 logical or ||
Both sides are false only returns false, otherwise it is all true.
1.5.4 Logical NOT!
Logic NOT (!) Operator also called negated, contrary to take a Boolean value, such as the opposite true value is false
var isOk = !true;
console.log(isOk); // false
demo
// 1. 逻辑与 && and 两侧都为true 结果才是 true 只要有一侧为false 结果就为false
console.log(3 > 5 && 3 > 2); // false
console.log(3 < 5 && 3 > 2); // true
// 2. 逻辑或 || or 两侧都为false 结果才是假 false 只要有一侧为true 结果就是true
console.log(3 > 5 || 3 > 2); // true
console.log(3 > 5 || 3 < 2); // false
// 3. 逻辑非 not !
console.log(!true); // false
练习
var num = 7;
var str = "我爱你~中国~";
console.log(num > 5 && str.length >= num); // true
console.log(num < 5 && str.length >= num); // false
console.log(!(num < 10)); // false
console.log(!(num < 10 || str.length == num)); // false