Home Web Front-end JS Tutorial JavaScript Advanced Programming Reading Notes (6) Operators in ECMAScript (2)_javascript skills

JavaScript Advanced Programming Reading Notes (6) Operators in ECMAScript (2)_javascript skills

May 16, 2016 pm 05:55 PM
ecmascript operator

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=2<1;
console.log(bResult);//false

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

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

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

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

var bResult=-1<"a";
console.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 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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the root operator in C language? What is the root operator in C language? Mar 06, 2023 pm 02:39 PM

In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values ​​nor output imaginary results.

Golang error: 'invalid use of ... operator' How to solve it? Golang error: 'invalid use of ... operator' How to solve it? Jun 24, 2023 pm 05:54 PM

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

What does % mean in Java What does % mean in Java Mar 06, 2023 pm 04:48 PM

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

Is es2017 es6 or es8? Is es2017 es6 or es8? Oct 27, 2022 pm 05:37 PM

es2017 is es8. The full name of es is "ECMAScript", which is a universal scripting language implemented according to the ECMA-262 standard. The version officially released in June 2017 is officially called ECMAScript2017 (ES2017). Because it is the 8th version of ECMAScript, it can Referred to as es8.

Analysis of the meaning and usage of += operator in C language Analysis of the meaning and usage of += operator in C language Apr 03, 2024 pm 02:27 PM

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

What is the meaning of '==' symbol in php What is the meaning of '==' symbol in php Mar 14, 2023 pm 07:05 PM

In PHP, the "==" symbol is a comparison operator that can compare whether two operands are equal. The syntax is "operand 1 == operand 2". The "==" operator compares and tests whether the variable on the left (expression or constant) has the same value as the variable on the right (expression or constant); it only compares the values ​​of the variables, not the data types. If the two values ​​are the same, it returns a true value; if the two values ​​are not the same, it returns a false value.

How to determine if two numbers are divisible in php How to determine if two numbers are divisible in php Jan 10, 2023 pm 03:12 PM

In PHP, you can use the "%" and "==" operators to determine whether two numbers are divisible; you only need to use the "%" operator to divide the two numbers to get the remainder, and then use the "==" operator Just judge whether the obtained remainder is 0. The syntax is "Number 1 % Number 2 == 0". If it is 0, it can be divisible. If it is not 0, it cannot be divisible.

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

See all articles