Home > Article > Web Front-end > What is the meaning of javascript logical operators
In JavaScript, logical operators are special symbols that connect statements into more complex statements. Logical operators can be used to represent logical operations such as "negation", "or", and "and"; in JavaScript There are three logical operators: "!", "&&" and "||".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
Logical operators are used to express ideas such as "and", "or", and "unless" in daily communication.
Logical Operators: In formal logic, logical operators or logical connectives connect statements into more complex statements.
For example, suppose there are two logical propositions, namely "It is raining" and "I am in the house". They can be turned into complex propositions: "It rained, and I was in the house" or "It didn't rain," or "If it rained, I was in the house." A new statement or proposition composed of two statements is called a compound statement or compound proposition.
The reason why "AND" and "OR" have two different forms of operators is that their operation priorities are different. Operators are used to perform program code operations and will perform operations on more than one operand item.
Logical operators in JavaScript
First of all, let’s talk about the rules for converting other data types to Boolean types:
null, undefined, 0, NaN, empty string conversion is false, and others are converted to true.
There are three logical operators in JavaScript:
1. Negation!
First convert the data into a Boolean value, and then negate it. The result is true or false
<script type="text/javascript"> var a = [1,2,3]; var b = "hello"; var obj = new Object(); var d; console.log(!""); console.log(!d); console.log(!a); console.log(!b); console.log(!obj); </script>
2. Logical AND &&
The logic in js is different from other languages. If the first operand is true (or can be converted to true), the calculation result is the second operand. If the first operand is false, the result is false (short circuit Calculation), the above rules are not followed for some special values.
<script type="text/javascript"> var a = [1,2,3]; var b = "hello"; var obj = new Object(); var d; console.log(true && 10);//第一个操作数是true,结果是第二个操作,也就是10 console.log(false && b);//第一个操作数是false,结果flase console.log(100 && false);//第一个操作数是100,结果flase console.log(undefined && false);//第一个操作数是undefined,结果undefined console.log(NaN && false);//第一个操作数是NaN,结果NaN console.log(null && false);//第一个操作数是null,结果null console.log('' && false);//第一个操作数是空串,结果空串 console.log(0 && 100);//结果是0 console.log(5 && 100);//100 console.log(a && b);//hello console.log(obj && 200);//200 </script>
3. Logical OR ||
If the first operand is not false, the result is the first operands, otherwise the result is the second operand. If the first operand can be converted to true, the result is the first operand
<script type="text/javascript"> var a = [1,2,3]; var b = "hello"; var obj = new Object(); var d; console.log(true || 10);//第一个操作数是true,结果是第一个操作,也就是true console.log(false || b);//第一个操作数是false,结果是第二个操作数b console.log(100 || false);//第一个操作数是100,结果100 console.log(undefined || 9);//第一个操作数是undefined转false,结果9 console.log(NaN || false);//第一个操作数是NaN转false,结果第二个操作数 console.log(null || a);//第一个操作数是null转false,结果a console.log('' || false);//第一个操作数是空串转false,结果第二操作数 console.log(0 || 100);//结果是100 console.log(5 || 100);//5 console.log(a || b);//a console.log(obj || 200);//obj </script>
Related recommendations: javascript learning tutorial
The above is the detailed content of What is the meaning of javascript logical operators. For more information, please follow other related articles on the PHP Chinese website!