Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript Type Conversions

JavaScript Type Conversions

JavaScript Type Conversions and some explanations to the Gary Bernhardt's wat lighting talk

Orlando Del Aguila

March 13, 2015
Tweet

More Decks by Orlando Del Aguila

Other Decks in Programming

Transcript

  1. var a, b; a = "bla"; b = "ble"; a

    + b; //=> "blable" a - b; //=> "NaN" a = "5"; b = "4"; a + b; //=> "54" a - b; //=> 1
  2. var obj = { valueOf: function valueOf() { console.log("valueOf"); return

    {}; // not a primitive }, toString: function toString() { console.log("toString"); return {}; // not a primitive } }; obj - 1; // valueOf // toString // error obj + 1; // valueOf // toString // error
  3. var func = function () { console.log('exec'); return { valueOf:

    function valueOf() { console.log("valueOf"); return {}; // not a primitive }, toString: function toString() { console.log("toString"); return {}; // not a primitive } }; }; func() + 1; // exec // valueOf // toString // error
  4. {} + [] //=> +[] == 0 [] + {}

    //=> '' + '[object Object]' == '[object Object]' [] - {} //=> 0 - NaN == NaN {} - [] //=> -[] == -0