created by Brian LeRoux & Andrew Lunny. sparodically uncurated by David Trejo.

2010 04 02 object to primitive coerce

    var foo = { 
        toString: function () { 
            return 5; 
        }, 
        valueOf: function () { 
            return "foo"; 
        } 
    }; 
    alert(foo.toString() + 1); // 6 (bad!) 
    alert(foo + 1);            // "foo1" (no good!) 
    alert(+foo);               // NaN (the worst!)

Thanks to Ajaxian for pointing out this amazingly detailed look at object to primitive coercion.

Fork me on GitHub