Same is true for JavaScript’s namesake, Java; Object has a toString method, so everything but primitives (int, long, etc.) must have a toString method (and primitives sort of have one too in a roundabout way).
I think JavaScript’s toString also serves another function, namely to have some form of fallback when doing operations on what should be incompatible types. [] + "", for instance; JavaScript will call toString() to do type conversion when the nearest matching type is a String.
Same is true for JavaScript’s namesake, Java;
Objecthas atoStringmethod, so everything but primitives (int,long, etc.) must have atoStringmethod (and primitives sort of have one too in a roundabout way).I think JavaScript’s
toStringalso serves another function, namely to have some form of fallback when doing operations on what should be incompatible types.[] + "", for instance; JavaScript will calltoString()to do type conversion when the nearest matching type is aString.