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

What are the javascript logical operators?

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

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 value true or false.

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 values ​​true or false. We can use the Boolean() function to determine whether the value of an expression (or variable) is true or false.

Example:

Execute the following code in the browser, and a pop-up layer showing true will pop up:

alert(Boolean(7 > 2));

This means that the result of 7 > 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

Logical AND operator&&, if the first operand is true, calculate The result is the second operand. If the first operand is false, the result is false (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 to true (not false), 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 is true or false.

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 get true:

console.log(!true);         // false  
console.log(!false);        // true 
console.log(!0);            // true
console.log(!"");           // true  
console.log(![1, 2, 3]);    // false

The number 0 can be converted into a Boolean value false, if you negate the result is true. ""The same applies to empty strings. It is first converted to false and then inverted to get true. The array [1, 2, 3] is converted into a Boolean value true, and its inversion is false.

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