Summary of various operators in JavaScript
The operators we are talking about here include arithmetic operators and logical operators, including Boolean and assignment, etc., we have carried out the JavaScript for everyone For a summary of the use of various operators, friends in need can refer to
Unary Operator
An operator that can only operate on one value is called a unary operator.
The unary operator is the simplest operator in ECMAScript.
1. Increment and decrement operators
The increment and decrement operators are directly borrowed from C, and each has two versions: prefix type and postposition type. Gu Mingsiyi, the prefixed type should be placed before the variable to be operated on, and the postfixed type should be placed after the variable to be operated on.
Pre-type:
var num1 = 1; var num2 = 2; var num3 = ++num1 + num2;//4
Post-type:
var num1 = 1; var num2 = 2; var num3 = num1++ + num2;//3
The above two pieces of code have different results. The reason is that there is a difference between post-increment and decrement and pre-increment and decrement. A very important difference is that post-increment-decrement operations are performed after the statements containing them are evaluated.
Applicable scope:
Preincrement and decrement and postincrement and decrement. All these four operators are applicable to any value. When applied to different values, this operator will convert the value like the Number() conversion function, and then add or subtract 1 after conversion.
2. Unary addition and subtraction operators
The unary addition and subtraction operators are mainly used for basic arithmetic operations and can also be used to convert data types (This operator will convert this value like the Number() conversion function).
Boolean operators
There are three Boolean operators: NOT (NOT), AND (AND), or (OR).
1. Logical NOT
The logical NOT operator is represented by an exclamation point (!) and can be applied to any value in ECMAScript. Regardless of the data type of the value, this operator returns a Boolean value.
Using two logical NOT operators at the same time will actually simulate the behavior of the Boolean() transformation function
2. Logical AND
Logical AND operator Represented by two ampersands (&&), it has two operands and can be applied to any type of operand. Logical AND is a short-circuit operation, that is, if the first operand evaluates to false, then the second operand will not be evaluated.
When both values are true, the result is true. When two values are one true and one false, the result is false. When both values are false, false is returned.
When one of the values is not a Boolean value: follow the following rules
If the first operand is false, return the first one;
When the first operand is true, return the second.
If the first operand is an object, return the second operand
var a = {b:1}; a && 'ss'//"ss"
If the second operand is an object, then only if the evaluation result of the first operand is true The object will only be returned if
'ss' && a//Object {b: 1}
If both operands are objects, the second operand will be returned
var c = {d:2}; c && a//Object {b: 1}
(1) If one operand is null, null will be returned
(2) If one of the operands is NaN, then NaN is returned
(3) If one of the operands is undefined, then undefined is returned
3. Logical OR
Similar to the logical AND operator, the logical OR operator is also a short-circuit operator. That is, if the first operand evaluates to true, the second operand will not be evaluated.
(1) If the first operand is true, return the first
(2) If the first operand is false, return the second
Multiplicative operator
ECMAScript defines 3 multiplicative operators: multiplication, division and modulo
Infinity*0//NaN 0/0//NaN Infinity/Infinity//NaN
Additive operators
1. Addition (convert to string)
Both operators are numerical values
Perform regular addition calculations.
Infinity + -Infinity//NaN
If one of the operands is a string
If both operators are strings, concatenate the second operator with the first operator
If If only one of the operators is a string, convert the other operand to a string, and then concatenate the two strings.
If the operand is an object, numeric value or Boolean value, call their toString() method to obtain the corresponding string value, and then apply the previous rules about strings. For null and undefined, call the String() function and obtain the strings "undefined" and "null" respectively.
2 + '' //"2"
2. Subtraction (convert numeric value)
If both operands are numeric values
Perform the regular arithmetic subtraction operation and return the result, if there is one operand is NaN, the result is NaN
Infinity - Infinity//NaN
If one of the operands is not a numeric value
If one of the operands is a string, Boolean value, null or undefined, first call Number( ) function converts it to a numeric value, and then performs the subtraction calculation according to the previous rules. If the result of the conversion is NaN, the result of the subtraction is NaN.
If one of the operands is an object, call the object's valueOf() method to obtain the value representing the object. If the resulting value is NaN, the result of the subtraction is NaN. If the object does not have a valueOf() method, its toString() method is called and the resulting string is converted into a numerical value.
5 - true//4
关系操作符
如果两个操作数都是数值,则执行数值比较
如果两个操作数都是字符串,则比较两个字符串对应的字符编码值
如果一个操作数是数值,则将另一个操作数转换为数值,然后执行数值比较
var result = '23' < '3'//true var result = '23' < 3//false
相等操作符
1.相等和不相等
先转换再比较
(1)如果有一个操作数是布尔值,则在比较相等性之前,先将其转换为数值
(2)如果有一个操作数是字符串,另一个操作数是数值,先将其转换为数值
(3)如果有一个操作数是对象,另一个不是,则调用对象的valueOf()方法,用得到的基本类型值按前面的基本规则进行比较
null和undefined是相等的
要比较相等性之前不能将null和undefined转换为任何其他值
如果两个操作数都是NaN,相等操作符也返回false,按规则,NaN不等于NaN
2.全等和不全等
仅比较而不转换
"55" !== 55 //true
条件操作符
variable = boolean_expression ? true_value : false_value
本质上,这段代码的含义就是基于对boolean_expression求值的结果,决定给变量variable赋什么值。如果求值结果为true,则给变量赋true_value;如果求值结果为false,则给变量variable赋false_value值。
赋值操作符
简单的赋值操作符由等号表示,其作用就是把右侧的值赋给左侧的变量。
逗号操作符
逗号操作符多用于声明多个变量;但除此之外,逗号操作符还用来赋值。在用于赋值时,逗号操作符总会返回表达式中的最后一项。
The above is the detailed content of Summary of various 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.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

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

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

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

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

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
