day-5.3 +=、-=、/=、*=、%= 运算符
相对于 直接的 +、-、*、/、% 运算符,其运算结果都不会改变原来的变量。
而 +=、-=、/=、*=、%= 这类运算符会改变运算符左边的变量的值;
比如;
下面的例子,有2个地方需要注意的:
1、每次计算都将运算结果重新赋值给a,
2、只有+=运算的时候,是对两个字符串变量直接进行了拼接操作,并未做隐式转换。其遵循 + 运算符运算的时候,运算符任何一边为字符串的时候,都会将另外一边转换成字符串再进行拼接的原理。
1 <body> 2 <div id="wrap"></div> 3 <script> 4 var a = "20", 5 b = "4"; 6 //a += b; //a输出 204,string,b输出 4,string 7 //a *= b ; //a输出 80,number,b输出 4,string 8 //a /= b; //a输出 5,number,b输出 4,string 9 //a %= b; //a输出 0,number,b输出 4,string 10 a -=b; //a输出16,number,b输出 4,string 11 console.log(a); 12 console.log(typeof a); 13 console.log(b); 14 console.log(typeof b); 15 </script> 16 </body>
浙公网安备 33010602011771号