What are the logical operators in JavaScript?
The logical operators of JavaScript are: 1. Logical AND "&&", which returns true only when both operands are true; 2. Logical OR "||", if both operands are true is true, or one of them is true, return true; 3. Logical not "!", convert the value of the operand into a Boolean value, and negate it.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Logical operators in javascript
Logical operators are usually used to combine multiple expressions. The result of a logical operator is a Boolean value, which can only have two results, not true. It's false. The following table lists the logical operators supported in JavaScript:
Operator | Name | Example |
---|---|---|
&& | Logical AND | x && y means if both x and y are true, then it is true |
|| | Logical OR | x || y means if either x or y is true, then it is true |
! | Logical not | !x means if x is not true, then it is true |
Logical AND operation
Logical AND operation (&&
) is an AND Boolean operation. Returns true only if both operands are true, otherwise returns false. The detailed description is shown in the table.
First operand | Second operand | Operation result |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Logical AND is a kind of short-circuit logic. If the expression on the left is false, the result will be short-circuited and returned directly, and the expression on the right will no longer be evaluated. . The operation logic is as follows:
Step 1: Calculate the value of the first operand (expression on the left).
Step 2: Detect the value of the first operand. If the value of the expression on the left is convertible to false (such as null, undefined, NaN, 0, "", false), then the operation will end and the value of the first operand will be returned directly.
Step 3: If the first operand can be converted to true, evaluate the second operand (the expression on the right).
Step 4: Return the value of the second operand.
Example 1
The following code uses logical AND operations to detect variables and initialize them.
var user; //定义变量 (! user && console.log("没有赋值")); //返回提示信息“没有赋值”
Equivalent to:
var user; //定义变量 if (! user){ //条件判断 console.log("变量没有赋值"); }
If the value of variable user is 0 or a false value such as an empty string is converted to a Boolean value, it will be false. Then when the variable is assigned a value, it will still prompt The variable has no value assigned. Therefore, when designing, you must ensure that the return value of the expression on the left side of the logical AND is a predictable value.
var user = 0; //定义并初始化变量 (! user && console.log("变量没有赋值")); //返回提示信息“变量没有赋值”
The expression on the right side should not contain valid operations such as assignment, increment, decrement, and function call, because when the expression on the left side is false, the expression on the right side is skipped directly, which will affect the subsequent operations. bring potential impact.
Logical OR operation
Logical OR operation||
is a Boolean OR operation. If both operands are true, or one of them is true, it returns true, otherwise it returns false. The details are shown in the figure.
First operand | Second operand | Operation result |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
逻辑或也是一种短路逻辑,如果左侧表达式为 true,则直接短路返回结果,不再运算右侧表达式。运算逻辑如下:
第 1 步:计算第一个操作数(左侧表达式)的值。
第 2 步:检测第一个操作数的值。如果左侧表达式的值可转换为 true,那么就会结束运算,直接返回第一个操作数的值。
第 3 步:如果第一个操作数可以转换为 false,则计算第二个操作数(右侧表达式)的值。
第 4 步:返回第二个操作数的值。
示例2
针对下面 4 个表达式:
var n = 3; (n == 1) && console.log(1); (n == 2) && console.log(2); (n == 3) && console.log(3); ( ! n ) && console.log("null");
可以使用逻辑或对其进行合并:
var n = 3; (n == 1) && console.log(1) || (n == 2) && console.log(2) || (n == 3) && console.log(3) || ( ! n ) && console.log("null");
由于&&运算符的优先级高于||运算符的优先级,所以不必使用小括号进行分组。不过使用小括号分组后,代码更容易阅读。
var n = 3; ((n == 1) && console.log(1)) || ((n == 2) && console.log(2)) || ((n == 3) && console.log(3)) || (( ! n ) && console.log("null")) ||
逻辑与和逻辑或运算符具有以下 2 个特点:
在逻辑运算过程中,临时把操作数转换为布尔值,然后根据布尔值决定下一步的操作,但是不会影响操作数的类型和最后返回结果。
受控于第一个操作数,可能不会执行第二个操作数。
逻辑非运算
逻辑非运算!
是布尔取反操作(NOT)。作为一元运算符,直接放在操作数之前,把操作数的值转换为布尔值,然后取反并返回。
示例3
下面列举一些特殊操作数的逻辑非运算返回值。
console.log( ! {} ); //如果操作数是对象,则返回false console.log( ! 0 ); //如果操作数是0,则返回true console.log( ! (n = 5)); //如果操作数是非零的任何数字,则返回false console.log( ! null ); //如果操作数是null,则返回true console.log( ! NaN ); //如果操作数是NaN,则返回true console.log( ! Infinity ); //如果操作数是Infinity,则返回false console.log( ! ( - Infinity )); //如果操作数是-Infinity,则返回false console.log( ! undefined ); //如果操作数是undefined,则返回true
示例4
如果对操作数执行两次逻辑非运算操作,就相当于把操作数转换为布尔值。
console.log( ! 0 ); //返回true console.log( ! ! 0 ); //返回false
逻辑与和逻辑或运算的返回值不必是布尔值,但是逻辑非运算的返回值一定是布尔值。
【推荐学习:javascript高级教程】
The above is the detailed content of What are the logical operators in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
