JavaScript 数据类型的转换

在涉及加法运算符(+)的数字和字符串表达式中,JavaScript 会把数字值转换为字符串。例如,假设有如下的语句:


x = "The answer is " + 42 // "The answer is 42"
y = 42 + " is the answer" // "42 is the answer"

在涉及其它运算符(译注:如下面的减号'-')时,JavaScript语言不会把数字变为字符串。例如(译注:第一例是数学运算,第二例是字符串运算):

"37" - 7 // 30
"37" + 7 // "377"


猜你喜欢

转载自blog.csdn.net/dianagreen7/article/details/79695152