search
HomeWeb Front-endJS TutorialSummary of various operators in JavaScript

Summary of various operators in JavaScript

Jul 04, 2017 pm 03:23 PM
javascriptjsSummary

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 = &#39;23&#39; < &#39;3&#39;//true
var result = &#39;23&#39; < 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!

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
JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor