📜  JavaScript比较和逻辑运算符

📅  最后修改于: 2020-09-27 04:16:11             🧑  作者: Mango

## JavaScript比较运算符

Operator Description Example
`==` Equal to: `true` if the operands are equal `5 == 5`
`!=` Not equal to: `true` if the operands are not equal `5 != 5`
`===` Strict equal to: `true` if the operands are equal and of the same type `5 === '5'`
`!==` Strict not equal to: `true` if the operands are equal but of different type or not equal at all `5 !== '5'`
`>` Greater than: `true` if the left operand is greater than the right operand `3 > 2`
`>=` Greater than or equal to: `true` if the left operand is greater than or equal to the right operand `3 >= 3`
`<` Less than: `true` if the left operand is less than the right operand `3 < 2`
`<=` Less than or equal to: `true` if the left operand is less than or equal to the right operand `2 <= 2`

### 示例1：等于运算符

``````let a = 5, b = 2, c = 'hello';

// equal to operator
console.log(a == 5);     // true
console.log(b == '2');   // true
console.log(c == 'Hello');  // false``````

### 示例2：不等于运算符

``````let a = 3, b = 'hello';

// not equal operator
console.log(a != 2); // true
console.log(b != 'Hello'); // true``````

### 示例3：严格等于运算符

``````let a = 2;

// strict equal operator
console.log(a === 2); // true
console.log(a === '2'); // false``````

`==`评估为`true` ，如果操作数是相等的，然而， `===`计算结果为`true`只有如果操作数是相等的，相同类型的

### 示例4：严格不等于运算符

``````let a = 2, b = 'hello';

// strict not equal operator
console.log(a !== 2); // false
console.log(a !== '2'); // true
console.log(b !== 'Hello'); // true``````

### 示例5：大于运算符

``````let a = 3;

// greater than operator
console.log(a > 2); // true``````

`>`如果左操作数大于右操作数，则结果为`true`

### 示例6：大于或等于运算符

``````let a = 3;

// greater than or equal operator
console.log(a >= 3); //true``````

### 示例7：小于运算符

``````let a = 3, b = 2;

// less than operator
console.log(a < 2); // false
console.log(b < 3;) // true``````

### 示例8：小于或等于运算符

``````let a = 2;

// less than or equal operator
console.log(a <= 3) // true
console.log(a <= 2); // true``````

## JavaScript逻辑运算符

Operator Description Example
`&&` Logical AND: `true` if both the operands/boolean values are true, else evaluates to `false` `true && false; // false`
`||` Logical OR: `true` if either of the operands/boolean values is `true`. evaluates to `false` if both are `false` `true || false; // true`
`!` Logical NOT: `true` if the operand is `false` and vice-versa. `!true; // false`

### 示例9：逻辑AND运算符

``````let a = true, b = false;
let c = 4;

// logical AND
console.log(a && a); // true
console.log(a && b);  // false

console.log((c > 2) && (c < 2)); // false``````

`&&`计算结果为`true` ，如果两个操作数是`true` ，否则计算结果为`false`

### 示例10：逻辑或运算符

``````let a = true, b = false, c = 4;

// logical OR
console.log(a || b); // true
console.log(b || b); // false
console.log((c>2) || (c<2)); // true``````

`||`如果两个操作数中的任何一个为`true`则结果为`true` 。如果两个操作数均为`false` ，则结果为`false`

### 示例11：逻辑非运算符

``````let a = true, b = false;

// logical NOT
console.log(!a); // false
console.log(!b); // true``````

`!`计算结果为`true` ，如果操作数是`false` ，反之亦然。