What is the meaning of javascript logical operators

WBOY
Release: 2022-03-24 16:48:19
Original
2001 people have browsed it

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

What is the meaning of javascript logical operators

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

What is the meaning of javascript logical operators

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>
Copy after login

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(&#39;&#39; && false);//第一个操作数是空串,结果空串
console.log(0 && 100);//结果是0
console.log(5 && 100);//100
console.log(a && b);//hello
console.log(obj && 200);//200
</script>
Copy after login

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(&#39;&#39; || false);//第一个操作数是空串转false,结果第二操作数
console.log(0 || 100);//结果是100
console.log(5 || 100);//5
console.log(a || b);//a
console.log(obj || 200);//obj
</script>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template