📜  JavaScript比较和逻辑运算符

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

在本教程中,您将在示例的帮助下了解“比较” 运算符和“逻辑” 运算符 。

JavaScript比较运算符

运算符比较两个值并返回一个布尔值: truefalse 。 运算符用于决策和循环。

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

如果操作数相等,则==等于true

注意 :在JavaScript中, ==为运算符,而=是一个赋值运算符。如果错误地使用=而不是== ,则可能会得到不需要的结果。


示例2:不等于运算符

let a = 3, b = 'hello';

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

如果操作数不相等,则!=等于true


示例3:严格等于运算符

let a = 2;

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

如果操作数相等且类型相同,则===得出true 。这里2‘2’是相同的数字,但数据类型不同。并且===还在比较时检查数据类型。


注意=====之间的区别在于:

==评估为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

如果操作数严格不相等,则!==等于true 。与严格等于===完全相反。

在上面的示例中, 2 != '2'给出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

如果左操作数大于或等于右操作数,则>=等于true


示例7:小于运算符

let a = 3, b = 2;

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

如果左操作数小于右操作数,则<true


示例8:小于或等于运算符

let a = 2;

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

如果左操作数小于或等于右操作数,则<=等于true


JavaScript逻辑运算符

逻辑运算符执行逻辑运算: ANDORNOT

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

注意:您也可以对数字使用逻辑运算符 。在JavaScript中,0为false ,所有非零值均为true


示例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 ,反之亦然。