Unary and binary operators

The PS: "+" and "-" both unary operators, binary operators also. 

First we look at what these results the following questions is:


when x = '1'time, the X-+1; the X-1-; + the X-; -x; ++the X-; typeof(the X-+ 1); typeof(the X- -1); typeof(+the X-); typeof(-the X-); typeof(++the X-); the result is how much each? Answer: X+ 1'd //'. 11' X. 1- //0 + X //. 1 the -X- //-1 ++ X //2 typeof(X +. 1)//'String' typeof(. 1-X)//'number' typeof(+x) //'number' typeof(-x) //'number' typeof(++x) //'number'

One yuan adder (+)

One yuan addition operator converts a digital operands (or NaN), and returns the converted number. If the operand itself is a number, returns this number directly.

+1     // => 1: Number of operating in and of itself, directly returns this number 
+ '1'   // => 1: converts a string to numeric 
+ '- 1' @ => -1: string converted to digital

Unary minus (-)

When "-" is used as a unary operator, it will be necessary to convert the operands to digital, and then changing the sign of the operation result.

-1    // => -1: operands and of itself, directly change the sign of the operation result 
-'1 ' @ => -1: string converted to digital, and then changing the sign of the operation result 
-'- 1' // => 1: converts a string of numbers, and then changing the sign of the operation result

Two yuan addition (+)

Two yuan addition operator "+" to make an addition to the two numbers can also be done string concatenation.

When two operands are numbers, or when the string is, the results will be apparent. In other cases, however, will have some of the necessary type conversions, and operator behavior depends on the type of the result of the conversion. Plus the conversion rule priority string concatenation, wherein if the operand is a string or a string into an object, the other operand is converted to strings, adding the connecting operation string. If neither operand is a string-like (string-like), the two operands are converted to a digital (or NaN3), followed by arithmetic addition.

Here are a few chestnuts:

1 + 1         @ => 2: adding 
'1' + '1'     @ => '11': connecting string 
'1' + 1       // string after digital conversion to a string: => '11' connection 
. 1 + {}        // => '. 1 [Object Object]': Object converted character string after the string is connected 
to true + to true   // => 2: after doing a Boolean value into a digital adder 
. 1 + null      // = > 1: null converted to do addition after 0 
. 1 + undefined // => NaN: undefined after conversion to do addition NaN

Special attention is required when the plus operator and numbers and strings used together, need to consider the addition of binding of the operational sequence. That is, the calculation result is dependent on the order of operations of operators, for example:

1 + 1 + '1';  // => '21'
1 +(1 + '1'); // => '111'

The first line has no parenthesis, "+" operator binding having from left to right, so that the two first digital adder for calculation, calculation results, and the connection string. In the second row, changing the order of operations in parentheses: 1 and the digital connection string, generates a new string and the new string and the number 1 is connected again to produce a final result.

Two yuan subtraction (-)

When "-" is used as a binary operator, it will be necessary to convert the operands to digital, and then the subtraction.

1-0     // => 1: subtraction 
'1'-0   @ => 1: after subtraction string into digital 
' 1 '-' 0 ' // => 1: after the digital string to subtraction

Increment (++)

Note that the "+" operator never string concatenation, it will always operand by 1 and converted to digital. No expression ++ x x = x + 1 summation exactly the same.

var X = '1' ;
 var Y = X + 1; // => '. 11': connecting string 
var Z = X ++; // => 2: 1 by the character string into digital

to sum up:

Some operators will do in JavaScript implicit type conversion. Unary operator "+", "-", "++", a binary operator "-" implicit operands are converted to digital, binary operator "+" special, when there is a character operand string, it will further converted to a string operand.

+ X '' // equivalent String (X) 
+ X      // equivalent Number (x)

 

Here is my number the public are welcome to our attention, we will study together and progress together

 

 

Reference article link: https: //www.javazhiyin.com/11742.html

Guess you like

Origin www.cnblogs.com/lxl0419/p/10953929.html