Skip to content

Comment on The WHY of WAT - understanding why 'JavaScript is Crazy'

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:

    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
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.