JS中数字和字符相加相减问题
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> </head> <body> </body> </html> <script type="text/javascript"> var a = 100; var b = "100"; alert(b- a); alert(typeof (b- a)); alert(a + b - a); alert(typeof (a + b - a)); alert(a + b); alert(typeof (a + b)); alert(a + b - b); alert(typeof (a + b - b)); alert(a + b - b - b); alert(typeof (a + b - b - b)); </script>
运行结果
0
number
100000
number
100100
string
100000
number
99900
number
由此可知道JS在进行这样的运算时 只会在2个值相加时优先判断为string类型 其他情况下都默认为number型运算!!!!