Home>Article>Web Front-end> What are the javascript logical operators?
Javascript logical operators include: 1. Logical AND operator "&&"; 2. Logical OR operator "||"; 3. Logical NOT operator "!", which will convert data into Boolean values. , and then negated, the result is true or false.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer
Logical operators in JavaScript can be used to determine the logical relationship between variables or values. Usually used for Boolean values, it will return a Boolean valuetrue
orfalse
.
Operator | Description |
---|---|
&& | and |
|| | or |
! | not |
The&&
,||
operators can use non-Boolean operands, and will return a non-Boolean value.
JavaScript provides a Boolean data type that only accepts the valuestrue
orfalse
. We can use theBoolean()
function to determine whether the value of an expression (or variable) istrue
orfalse
.
Example:
Execute the following code in the browser, and a pop-up layer showingtrue
will pop up:
alert(Boolean(7 > 2));
This means that the result of7 > 2
is true. Of course, 7 is inherently greater than 2. If it is the other way around,7 > 2
, then the browser will display false.
Logical AND operator&&
, if the first operand istrue
, calculate The result is the second operand. If the first operand isfalse
, the result isfalse
(except for special values).
Example:
console.log(true && true); // true 操作数为true则结果为第二个操作数 console.log(true && false); // false console.log(true && 10); // 10 console.log(true && (4 > 7)); // false console.log(false && 10); // false 操作数为false则结果为false console.log(false && ""); // false console.log(" " && 0); // 0 console.log(2 && 7); // 7
Expression that will be converted to false:
null
NaN
0
""
,''
, ``)undefined
Logical OR||
Operator, if the first operand can be converted totrue
(notfalse
), the result is the first operand, otherwise the result is the second operand.
Example:
console.log(true || true); // true 第一个操作数为true则结果为第一个操作数 console.log(true || false); // true console.log(true || 10); // true console.log(true || (4 > 7)); // true console.log(false || 10); // 10 第一个操作数不是true,则结果为第二个操作数 console.log(false || ""); // console.log(false || 0); // 0 console.log(0 || 7); // 7
Logical NOT operator!
Operator, first Convert the data into a Boolean value and then negate it. The result istrue
orfalse
.
Example:
For example,true
is originally a Boolean value, and if it is inverted,false
will be obtained.false
If you negate it, you will gettrue
:
console.log(!true); // false console.log(!false); // true console.log(!0); // true console.log(!""); // true console.log(![1, 2, 3]); // false
The number0
can be converted into a Boolean valuefalse
, if you negate the result istrue
.""
The same applies to empty strings. It is first converted tofalse
and then inverted to gettrue
. The array[1, 2, 3]
is converted into a Boolean valuetrue
, and its inversion isfalse
.
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of What are the javascript logical operators?. For more information, please follow other related articles on the PHP Chinese website!