JavaScript Advanced Programming Reading notes (vi) operators in ECMAScript (ii) _javascript tips

Source: Internet
Author: User
Tags first string numeric value
2.9.5, additive operator
Additive operators (that is, plus and minus signs) are usually the simplest operators, but in ECMAScript, each additive operator has a large number of special behaviors.

1. Addition Operator:
Copy Code code as follows:

var iresult=1+2;
Console.log (Iresult);//outputs 3

Particularity:

A certain op is Nan, and 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 the two operands are strings, concatenate the second string onto the first string
If only one op is a string, the other is converted to a string, and the result is a string of two strings connected
Example:
Copy Code code 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 code as follows:

var iresult=2-1;
Console.log (Iresult);//1

Particularity:

A certain op is Nan, and 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 all two 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 code 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 <, >, <=, >= perform a two-digit comparison operation and return a Boolean value. If two operands are strings, each compares the ASC code of two strings, and if only one is a string, converts the string to a number and compares it with the following example:
Copy Code code 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= < "2";
Console.log (bresult);//true

var bresult=13< "2";
Console.log (bresult);//false

var bresult=-1< "a";
Console.log (bresult);//false

In the 17th line of code, "A" is returned as a number, and any relational operation that contains Nan returns false.

2.9.7, equal sex operators
1, equal sign and non equal sign

In ECMAScript, both the equal sign (= =) and the non-equal sign (!=) return a Boolean value. To determine whether two counts are equal, the two counts are converted in type, and the conversion rules are as follows:

If an operand is a Boolean value, convert it to a numeric value before checking for equality. False converts into 0,true converted to 1.
If one of the operands is a string, and the other is a number, try to convert the string to a number before checking for equality.
If one of the operands is an object, and the other is a string, try to convert the object to a string before checking for equality.
If one of the operands is an object, and the other is a number, try converting the object to a number before checking for equality.
In comparison, the operators also follow the column rules:

Value null and undefined equal
You cannot convert null and undefined to other values when checking for equality.
If one of the operands is Nan, the equal sign returns false, and a non-equal sign returns TRUE. Important: Even if the two operands are Nan, the equals sign returns false because, according to the rule, Nan is not equal to Nan.
If the two operands are objects, they are compared to their reference values. If two operands are counted to the same object, then the equal sign returns TRUE, otherwise the two operands are unequal.
Example:

Copy Code code 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, full equals and non-full equals

Equal and non-equal equals are all equals and not equals. The two operators do the same as equals and non equals, except that they do not perform type conversions until the equality is checked. The full sign is represented by three equal signs (= = =), which is represented by an exclamation point plus two equals sign (!==) and returns true only if the type conversion shipping count is equal. For example:
Copy Code code as follows:

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

2.9.8, conditional operators
The conditional operator is the same as in other languages: Varialbe=boolean_expression?true_value:false_value;
Example:
Copy Code code 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 operators
A simple assignment operator is implemented by an equal sign (=), except that the value to the right of the equal sign is assigned to the variable to the left of the equal sign, for example:

var inum=10;
A compound assignment operation is implemented by a multiplicative, additive, or displacement operator plus an equal sign (=). These assignment operators are abbreviations for the following common situations:
Copy Code code as follows:

var inum=10;
inum=inum+10;

Equal to
var inum=10;
inum+=10;

Each of the major arithmetic operations and several other operations have a composite assignment operator:

Multiplication/Assignment (*=)
Division/Assignment (/=)
Modulo/Assignment (%=)
Addition/Assignment (+ =)
Subtraction/Assignment (-=)
Move left/Assignment (<<=)
Signed Right Move/assignment (>>=)
Unsigned Right Shift/assignment (>>>=)
2.9.10, comma operator
You can perform multiple operations in a single statement with the comma operator. For example:

var inum=1,inum2=2,inum3=3;
The comma operators are most commonly used in variable declarations.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.