Why are JS-hermit type conversions '1'+1, '1'-1, ++'1' different?

当 x=’1’时,
x+1
x-1
+x
-x
++x
typeof(x+1)
typeof(x-1)
typeof(+x)
typeof(-x)
typeof(++x)

  What are the results?

Answer:

x+1 //’11’
x-1 //0
+x//1
-x//-1
++x //2
typeof(x+1) //’string’
typeof(x-1) //’number’
typeof(+x) //’number’
typeof(-x) //’number’
typeof(++x) //’number’

  First of all, " + " and " - " are unary operators as well as binary operators .

  Unary addition (+)

  The unary addition operator converts the operand to a number (or NaN ) and returns the converted number. If the operand itself is a number, this number is returned directly.

+1  // => 1 : The operand itself is a number, then return the number directly

+ ' 1 '  // => 1 : convert string to number

+ ' -1 '  // => -1 : convert string to number

 

Unary Subtraction ( - )

When " - " is used as a unary operator, it converts the operands to numbers as necessary, and then changes the sign of the result.

 -1 // => -1 : the operand itself is a number, change the sign of the result of the operation

- ' 1 ' // =>  -1 : Convert the string to a number , and then change the sign of the result of the operation 

-'-1' // =>  1 : Convert the string to a number , and then change the sign of the result of the operation

Binary Addition (+)

The binary addition operator " + " can add two numbers and concatenate strings:

When both operands are numbers or strings, the result of the calculation is obvious. In other cases, however, some necessary type conversions are made, and the behavior of the operator depends on the result of the type conversion. The conversion rules of the plus sign give priority to string concatenation. If one of the operands is a string or an object converted to a string, the other operand will be converted to a string, and the addition will perform a string concatenation operation. If neither operand is string-like , then arithmetic addition is performed. Technically, the addition operator behaves as follows: If one of the operands is an object, the object is converted to a primitive class value following the object-to-primitive conversion rules. After the object-to-primitive conversion, the If one of the operands is a string, the other operand is also converted to a string and then concatenated. Otherwise, both operands are converted to numbers (or NaN ), and then the addition operation is performed.

Here are some examples:

1 + 2 // => 3: 加法

"1" + "2" // => "12": 字符串连接

"1" + 2 // => "12": 数字转换为字符串后进行字符串连接

1 + {} // => "1[object Object]": 对象转换为字符串后进行字符串连接

true + true // => 2: 布尔值转换为数字后做加法

2 + null // => 2: null转换为0后做加法

2 + undefined// => NaN: undefined转换为NaN后做加法

  需要特别注意的是,当加号运算符和字符串和数字一起使用时,需要考虑加法的结合性的对运算顺序的影响。也就是说,运算结果是依赖于运算符的运算顺序的,比如: 

1 + 2 + " blind mice"; // => "3 blind mice"

1 +2 + " blind mice"; // => "12 blind mice"

  第一行没有圆括号,+”运算符具有从左至右的结合性,因此两个数字首先进行加法计算,计算结果和字符串进行连接。在第二行中,圆括号改变了运算顺序:数字2和字符串连接,生成一个新字符串,然后数字1和这个新字符串再次连接,生成了最终结果。

 

(-)

-”用做元运算符时,它会根据需要把操作数转换为数字,然后再进行减运算

1-0 // => 1:

'1'-0  => 1: 字符串转为数字后进行减运算

'1'-'0' => 1: 字符串转为数字后进行减运算

递增(++

需要注意的是,++”运算符从不进行字符串连接操作,它总是会将操作数转换为数字并增1。表达式++x并不总和x=x+1完全一样

    var x='1';

    var y=x+1; //y的值是‘11

    var z=++x;//z的值是2

     typeof(y));// "string"

    typeof(z));//"number"

 

总结:

JavaScript中的某些运算符会做隐式的类型转换,有时用于类型转换。一元运算符+”,“-”,“++”,二元元算法“-”都会把操作数隐式的转换为数字二元运算符+比较特殊,当有操作数是字符串,它将会把另外一个操作数转换为字符串。

x + ""   // 等价于Stringx

+x      // 等价于Numberx

x=1时,

x+1

x-1

+x

-x

++x

typeof(x+1)

typeof(x-1)

typeof(+x)

typeof(-x)

typeof(++x)

的结果分别是多少?

答案

x+1 //11

x-1 //0

+x//1

-x//-1

++x //2

typeof(x+1) //string

typeof(x-1) //number

typeof(+x) //number

 

typeof(-x) //number

 

typeof(++x) //number

 

 

 

首先,+”和“-”是一元运算符,也是二元运算符

一元加法(+)

一元加法运算符把操作数转换为数字(或者NaN),并返回这个转换后的数字。如果操作数本身就是数字,则直接返回这个数字。

+1  // => 1: 操作数本身就是数字,则直接返回这个数字

+1 // => 1: 字符串转换为数字

+-1 // => -1: 字符串转换为数字

 

一元减法(-

-”用做一元运算符时,它会根据需要把操作数转换为数字,然后改变运算结果的符号。

 -1 // => -1: 操作数本身就是数字,改变运算结果的符号

-1 // => -1: 字符串转换为数字,改变运算结果的符号

-'-1'// => 1: 字符串转换为数字,再改变运算结果的符号

元加法(+)

二元加法运算符+”可以对两个数字做加法,也可以做字符串连接操作:

当两个操作数都是数字或都是字符串的时候,计算结果是显而易见的。然而对于其他情况来说,则要进行一些必要的类型转换,并且运算符的行为依赖于类型转换的结果。加号的转换规则优先考虑字符串连接,如果其中一个操作数是字符串或者转换为字符串的对象,另外一个操作数将会转换为字符串,加法将进行字符串的连接操作。如果两个操作数都不是类字符串(string-like)的,那么都将进行算术加法运算。 从技术上讲,加法操作符的行为表现为: ·如果其中一个操作数是对象,则对象会遵循对象到原始值的转换规则转换为原始类值,在进行了对象到原始值的转换后,如果其中一个操作数是字符串的话,另一个操作数也会转换为字符串,然后进行字符串连接。否则,两个操作数都将转换为数字(或者NaN),然后进行加法操作。

这里有一些例子:

1 + 2 // => 3: 加法

"1" + "2" // => "12": 字符串连接

"1" + 2 // => "12": 数字转换为字符串后进行字符串连接

1 + {} // => "1[object Object]": 对象转换为字符串后进行字符串连接

true + true // => 2: 布尔值转换为数字后做加法

2 + null // => 2: null转换为0后做加法

2 + undefined// => NaN: undefined转换为NaN后做加法

  需要特别注意的是,当加号运算符和字符串和数字一起使用时,需要考虑加法的结合性的对运算顺序的影响。也就是说,运算结果是依赖于运算符的运算顺序的,比如: 

1 + 2 + " blind mice"; // => "3 blind mice"

1 +2 + " blind mice"; // => "12 blind mice"

  第一行没有圆括号,+”运算符具有从左至右的结合性,因此两个数字首先进行加法计算,计算结果和字符串进行连接。在第二行中,圆括号改变了运算顺序:数字2和字符串连接,生成一个新字符串,然后数字1和这个新字符串再次连接,生成了最终结果。

 

(-)

-”用做元运算符时,它会根据需要把操作数转换为数字,然后再进行减运算

1-0 // => 1:

'1'-0  => 1: 字符串转为数字后进行减运算

'1'-'0' => 1: 字符串转为数字后进行减运算

递增(++

需要注意的是,++”运算符从不进行字符串连接操作,它总是会将操作数转换为数字并增1。表达式++x并不总和x=x+1完全一样

    var x='1';

    var y=x+1; //y的值是‘11

    var z=++x;//z的值是2

     typeof(y));// "string"

    typeof(z));//"number"

 

总结:

JavaScript中的某些运算符会做隐式的类型转换,有时用于类型转换。一元运算符+”,“-”,“++”,二元元算法“-”都会把操作数隐式的转换为数字二元运算符+比较特殊,当有操作数是字符串,它将会把另外一个操作数转换为字符串。

x + ""   // 等价于Stringx

+x      // 等价于Numberx

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324842234&siteId=291194637