Home>Article>Web Front-end> What are the javascript logical operators?

What are the javascript logical operators?

青灯夜游
青灯夜游 Original
2021-06-15 16:53:09 3944browse

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.

What are the javascript logical operators?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer

javascript logical operator

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 valuetrueorfalse.

Operator Description
&& and
|| or
! not

The&&,||operators can use non-Boolean operands, and will return a non-Boolean value.

Boolean value

JavaScript provides a Boolean data type that only accepts the valuestrueorfalse. We can use theBoolean()function to determine whether the value of an expression (or variable) istrueorfalse.

Example:

Execute the following code in the browser, and a pop-up layer showingtruewill pop up:

alert(Boolean(7 > 2));

This means that the result of7 > 2is 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

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
  • Empty string ("",'', ``)
  • undefined

Logical OR operator

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

Logical NOT operator!Operator, first Convert the data into a Boolean value and then negate it. The result istrueorfalse.

Example:

For example,trueis originally a Boolean value, and if it is inverted,falsewill be obtained.falseIf 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 number0can be converted into a Boolean valuefalse, if you negate the result istrue.""The same applies to empty strings. It is first converted tofalseand 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn