JavaScript Type Conversions

JavaScript Type Conversions

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

A0574e5093df6afd68f1c109c5a9bf22?s=128

Orlando Del Aguila

March 13, 2015
Tweet

Transcript

  1. JavaScript Type Conversions

  2. JavaScript Primitives yes, js has primitives

  3. 1. undefined 2. null 3. number 4. string 5. boolean

  4. 1. undefined 2. null yep, is not an object 3.

    number 4. string 5. boolean
  5. JavaScript Binary Operators we are going to address only -

    and +
  6. On doubt check the spec

  7. http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.1 http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.2

  8. + Operator

  9. If lprim or rprim are strings, then concatenate lprim and

    rprim and return the result
  10. - Operator

  11. ToNumber(lprim) - ToNumber(rprim)

  12. Examples

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

    + b; //=> "blable" a - b; //=> "NaN" a = "5"; b = "4"; a + b; //=> "54" a - b; //=> 1
  14. 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
  15. 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
  16. {} + [] //=> +[] == 0 [] + {}

    //=> '' + '[object Object]' == '[object Object]' [] - {} //=> 0 - NaN == NaN {} - [] //=> -[] == -0
  17. None
  18. WAT https://www.destroyallsoftware.com/talks/wat