search
HomeWeb Front-endJS TutorialJavaScript Advanced Programming Reading Notes (6) Operators in ECMAScript (2)_javascript skills

2.9.5. Additive operators
Additive operators (i.e. plus sign and minus sign) are usually the simplest operators, but in ECMAScript, each additive operator has a large number of special behaviors.

1. Addition operator:

Copy code The code is as follows:

var iResult=1 2;
console.log(iResult);//outputs 3

Speciality:

A certain operand is NaN, the result is NaN
Infinity plus Infinity, the result is Infinity
-Infinity plus -Infinity, the result is -Infinity
Infinity plus -Infinity, the result is NaN
If both operands are strings, put the second character String concatenation to the first string
If only one operand is a string, convert the other operand to a string, and the result is a string concatenated into two strings
Example:
Copy code The code is as follows:

var iResult2=NaN 1;
console.log(iResult2) ;//NaN

var iResult3=Infinity Infinity;
console.log(iResult3);//Infinity

var iResult4=-Infinity-Infinity;
console.log( iResult4);//-Infinity

var iResult5="abc" "bcd";
console.log(iResult5);//abcbcd

var iResult6=5 "5";
console.log(iResult6);//55

2. Subtraction operator:
Copy code The code is as follows:

var iResult=2-1;
console.log(iResult);//1

Speciality:

A certain operand is NaN, the result is NaN
Infinity minus Infinity, the result is NaN
-Infinity minus-Infinity, the result is NaN
Infinity minus-Infinity, the result is Infinity
-Infinity minus -Infinity, the result is -Infinity
If both operands are strings, the result is NaN
If only one operand is a string, convert the string to a number and then perform the operation
Example:
Copy code The code is as follows:

var iResult2=NaN-1;
console.log(iResult2);//NaN

var iResult3=Infinity-Infinity;
console.log(iResult3);//NaN

var iResult4=-Infinity-(- Infinity);
console.log(iResult4);//NaN

var iResult5=-Infinity-Infinity;
console.log(iResult5);//-Infinity

var iResult6=Infinity-(-Infinity);
console.log(iResult6);//Infinity

var iResult7="abc"-"a";
console.log(iResult7); //NaN

var iResult8="5"-5;
console.log(iResult8);//0

var iResult9="a"-5;
console .log(iResult9);//NaN

2.9.6. Relational operators
Relational operators , = execute two numbers The comparison operation returns a Boolean value. If both operands are strings, compare the ASC codes of the two strings one by one. If only one of the operands is a string, convert the strings into numbers and compare them. The example is as follows:
Copy code The code is as follows:

var bResult=2console.log(bResult);//false

var bResult="B"console.log(bResult);//true

var bResult="b"console.log(bResult);//false

var bResult="13"console.log(bResult);//true

var bResult =13console.log(bResult);//false

var bResult=-1console.log(bResult);//false

In the code on line 17, NaN is returned when "a" is converted to a number, and any relational operation containing NaN must return false.

2.9.7. Equality operator
1. Equal sign and non-equal sign

In ECMAScript, the equal sign (==) and the non-equal sign (!=) are both Returns a Boolean value. To determine whether two operands are equal, both operands will undergo type conversion. The conversion rules are as follows:

If an operand is a Boolean value, convert it to a numeric value before checking for equality. false is converted to 0 and true is converted to 1.
If one operand is a string and the other is a number, try to convert the string to a number before checking for equality.
If one operand is an object and the other is a string, try to convert the object to a string before checking for equality.
If one operand is an object and the other is a number, try to convert the object to a number before checking for equality.
When making comparisons, operators also follow the rules of return:

The values ​​null and undefined are equal
When checking equality, null and undefined cannot be converted to other values.
If an operand is NaN, the equal sign will return false, and the non-equal sign will return true. Important: Even if both operands are NaN, the equal sign still returns false because according to the rules, NaN does not equal NaN.
If both operands are objects, then their reference values ​​are compared. If the two operands refer to the same object, then the equal sign returns true, otherwise the two operands are not equal.
Example:

Copy code The code is as follows:

console.log( null==undefined);//true
console.log("NaN"==NaN);//false
console.log(5==NaN);//false
console.log( NaN==NaN);//false
console.log(NaN!=NaN);//true
console.log(false==0);//true
console.log(true= =1);//true
console.log(true==2);//false
console.log(undefined==0);//false
console.log(null==0 );//false
console.log("5"==5);//true

2. Congruent and non-congruent signs

Equal sign Similar operators to the non-equal sign are the equal sign and the non-equal sign. These two operators do the same as the equal sign and the not equal sign, except that they do not perform a type conversion before checking for equality. The congruent sign is represented by three equal signs (===), and the non-congruent sign is represented by an exclamation mark plus two equal signs (!==). True is returned only if the operands are equal without type conversion. For example:
Copy code The code is as follows:

console.log("55"==55 );//true
console.log("55"===55);//false
console.log("55"!=55);//false
console.log(" 55"!==55);//true

2.9.8, conditional operator
Conditional operator is the same as in other languages: variablebe=boolean_expression?true_value:false_value;
Example:
Copy code The code is as follows:

function Max(iNum1,iNum2){
return iNum1>=iNum2?iNum1:iNum2;
}
console.log(Max(1,3));//3
console.log(Max(3,1));/ /3

2.9.9. Assignment operator
The simple assignment operator is implemented by the equal sign (=), which just assigns the value on the right side of the equal sign to the variable on the left side of the equal sign, for example:

var iNum=10;
The compound assignment operation is implemented by the multiplicative operator, additive operator or displacement operator plus the equal sign (=). These assignment operators are shorthand for the following common cases:
Copy code The code is as follows:

var iNum=10;
iNum=iNum 10;

//Equivalent to
var iNum=10;
iNum =10;

each The main arithmetic operations and several other operations have compound assignment operators:

Multiplication/assignment (*=)
Division/assignment (/=)
Modulo/assignment (%=)
Addition/assignment (=)
Subtraction/assignment (-=)
Left shift/assignment (Signed right shift/assignment (>>=)
Unsigned right shift/assignment (>>>=)
2.9.10, comma operator
Use the comma operator to perform multiple operations in one statement. For example:

var iNum=1,iNum2=2,iNum3=3;
The comma operator is most commonly used in variable declarations.
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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),