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

WBOY
WBOYOriginal
2016-05-16 17:55:541248browse

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:
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