Comment on The WHY of WAT - understanding why 'JavaScript is Crazy'Comments−vmind14yThis is only part of the why. The other part is the valueOf function which you can specify in order to control what the value of an object is when coerced.So for instance: var arr1 = [1,2,3], arr2 = [1,2,3,4]; console.log(arr1 + arr2); // "1,2,31,2,3,4" arr1.valueOf = function () { return this.length; }; arr2.valueOf = arr1.valueOf; console.log(arr1 + arr2); // 7
Comments
This is only part of the why. The other part is the valueOf function which you can specify in order to control what the value of an object is when coerced.
So for instance: