Javascript’s type coercion is rather insane, yes, but there is an actual, practical reason it’s done. JS, having been designed to be run in web browsers, wants to avoid blowing up and crashing at all costs. If it gets an unusual type comparison, usually the result of a bug, it tries to return something, such that the script can continue running if at all possible. In JS’ mentality, keeping a page running, even if it might not completely function properly, is preferable to throwing an unhandled exception and completely crashing it.
Whether or not that is the right approach is debatable, but there is at least some logic to it. Personally, I think that the proliferation of Node letting JS run outside of browsers exacerbates a lot of JS’ issues, but TypeScript does a lot to make it look like a more sensible language.
Javascript’s type coercion is rather insane, yes, but there is an actual, practical reason it’s done. JS, having been designed to be run in web browsers, wants to avoid blowing up and crashing at all costs. If it gets an unusual type comparison, usually the result of a bug, it tries to return something, such that the script can continue running if at all possible. In JS’ mentality, keeping a page running, even if it might not completely function properly, is preferable to throwing an unhandled exception and completely crashing it.
Whether or not that is the right approach is debatable, but there is at least some logic to it. Personally, I think that the proliferation of Node letting JS run outside of browsers exacerbates a lot of JS’ issues, but TypeScript does a lot to make it look like a more sensible language.