I think that on the whole, JavaScript is a decent language. Its certainly not worse than its cousins, Ruby and Python. But I also think that automatic type coercion is a really bad idea. It doesn't give you much, and it leads to a lot of unexpected behavior. While I agree that [] + {} is not a reasonable line to compute, it should be a TypeError. The fact that JavaScript will happily process the request and continue with your program means that any actual errors will appear later, and the offending line is not clear. There have been plenty of times where I accidentally coerced an object to [object Object] and all of those times it was a bug in my code. An error at the source would have saved me a lot of time.
Next time on Mythbusters: an even bigger waste of memory by passing these objects to an ES6 Map as keys, meaning they themselves cannot be garbage collected and generating infinite duplicate cached values. What fun!
Thanks for your feedback, @nepeckman
.
I think that type coercion is due to historic reasons requiring that a browser should not crash (as much as possible) neither during HTML markup interpretation nor during JavaScript code execution
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think that on the whole, JavaScript is a decent language. Its certainly not worse than its cousins, Ruby and Python. But I also think that automatic type coercion is a really bad idea. It doesn't give you much, and it leads to a lot of unexpected behavior. While I agree that
[] + {}
is not a reasonable line to compute, it should be aTypeError
. The fact that JavaScript will happily process the request and continue with your program means that any actual errors will appear later, and the offending line is not clear. There have been plenty of times where I accidentally coerced an object to[object Object]
and all of those times it was a bug in my code. An error at the source would have saved me a lot of time.Truly, for example when you index into a hashmap object with another object instead of its property.
Next time on Mythbusters: an even bigger waste of memory by passing these objects to an ES6
Map
as keys, meaning they themselves cannot be garbage collected and generating infinite duplicate cached values. What fun!Thanks for your feedback, @nepeckman .
I think that type coercion is due to historic reasons requiring that a browser should not crash (as much as possible) neither during HTML markup interpretation nor during JavaScript code execution