关于“5”-1& Javascript中的“5”+1(加号和减号) [英] About "5"-1 & "5"+1 in Javascript (plus and minus signs)
问题描述
我在Javascript中读了一本关于运算符的书,这让我很困惑。
I read a book about operators in Javascript, and this confused me.
console.log("5"+1);
这会将5作为字符串。因此结果将是 51
。
This would make "5" as a string. So the result would be 51
.
console.log("5"-1);
此结果将是 4
。我知道它将5
转换为 5
,但为什么它不显示 undefined
为字符串减去数字?
This result would be 4
. I know it converts "5"
to 5
, but why it isn't shown undefined
as "a string minus a number"?
更新:那么其他语言怎么样?他们是否更受限制?
Update: So how about other languages? Are they more restrict?
推荐答案
可悲的是,人们希望JavaScript能够在早期的时候利用Java的成功促销和自Java使用以来,字符串连接被加上。
Sadly, it was expected from JavaScript to ride on Java's success for promotion on its early days and the plus for string concatenation was adopted since Java used it.
因此JavaScript努力将字符串强制转换为数字你真的这么做,只是因为字符串的加号所以......好吧......
So JavaScript tries hard to coerce strings into numbers for you, it really does, its just that the plus was taken for strings so....well...
虽然Javascript有很多实力,但它是在10天内完成的并且有许多像这样的搞笑方面,请查看喜剧黄金
While Javascript has many strenghts it was made in 10 days and has many hilarious aspects like this one, check this comedy gold
这篇关于关于“5”-1& Javascript中的“5”+1(加号和减号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!