While experimenting with the Object.toString()default behavior of the function , I noticed that string concatenations like the ones below are predictably called toString()for targets:
var x = { toString: () => "one" };
var y = { toString: () => "two" };
var output = 'x is ' + x + ' while y is ' + y;
console.log(output);
However, the same is not observed when toString()redefined in prototypes Numberand Boolean, for example. To obtain the desired result, you must "force" call the call toString():
Number.prototype.toString = () => "42";
Boolean.prototype.toString = () => "whatever you wish";
var a = 1;
console.log('The answer is ' + a);
console.log('The answer is ' + a.toString());
var b = true;
console.log('Girl you know it\ ' + b);
console.log('Girl you know it\ ' + b.toString());
This is consistent across browsers (tested on Chrome, Firefox, and Edge), so I assume this is standard behavior. Where is this documented? Is there a list of standard objects that receive special processing during string concatenations?