Suppose that instead of 5, there is a 55-bit integer. Now the promotion caused data loss. In any case, JS promotes implicitly to string because it just does. If you really want it to error, you can probably replace Object.prototype.toString() with your own implementation that throws an error.
Suppose that instead of 5, there is a 55-bit integer.
Pretty sure ints are 32 bits. And if you did write a 55 bit integer as a literal, then no, it's not promotion anymore and yes, an error should be thrown.
In any case, JS promotes implicitly to string because it just does.
So it's totally arbitrary then. Seems like an awful design decision.
Most JavaScript implementations handle integers upto... 52 bits (?) as integers. 53 fits into a double without precision loss, IIRC. After that you start getting problems.
1
u/[deleted] Aug 26 '20
Suppose that instead of 5, there is a 55-bit integer. Now the promotion caused data loss. In any case, JS promotes implicitly to string because it just does. If you really want it to error, you can probably replace
Object.prototype.toString()with your own implementation that throws an error.