I read a book about operators in Javascript, and this confused me.
console.log("5"+1);
This would make "5" as a string. So the result would be 51
.
console.log("5"-1);
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?
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.
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...
While Javascript has many strenghts it was made in 10 days and has many hilarious aspects like this one, check this comedy gold
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With