Slide 1

Slide 1 text

JavaScript Type Conversions

Slide 2

Slide 2 text

JavaScript Primitives yes, js has primitives

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

1. undefined 2. null yep, is not an object 3. number 4. string 5. boolean

Slide 5

Slide 5 text

JavaScript Binary Operators we are going to address only - and +

Slide 6

Slide 6 text

On doubt check the spec

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

+ Operator

Slide 9

Slide 9 text

If lprim or rprim are strings, then concatenate lprim and rprim and return the result

Slide 10

Slide 10 text

- Operator

Slide 11

Slide 11 text

ToNumber(lprim) - ToNumber(rprim)

Slide 12

Slide 12 text

Examples

Slide 13

Slide 13 text

var a, b; a = "bla"; b = "ble"; a + b; //=> "blable" a - b; //=> "NaN" a = "5"; b = "4"; a + b; //=> "54" a - b; //=> 1

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

WAT https://www.destroyallsoftware.com/talks/wat