Kottans

YDKJS. Types Grammar

YDKJS. Types Grammar

๐ŸŸจ

Lapka

YDKJS. Roadmap

Tasks

Tasks

Today in program...

Why typesโ“

Static vs. Dynamic typing

Strong vs. Weak typing

๐Ÿ‘ฎ ๐Ÿš”

JS hasโ“

JS has is โ€“

Old browser

Data types in JS

ECMAScript Data Types and Values

Data types in JS

Values & Operations

ECMAScript Data Types and Values

Primitives

Everything else โ€“ Object

typeof

Magic (Primitive) Methods ๐Ÿ”ฎ

        14.22.toString() // "14.22"
      
        // JS Under The Hood
        new Number(14.22).toString() // "14.22"
      

Boxing Wrapper ๐Ÿ“ฆ

        const str = new String('Kottans ๐Ÿˆโ€โฌ›')
        const n = new Number(1422)
        const hasSomething = new Boolean(false)
        
        
      

Object โ€“ always reference

Literals for References

        new Object()
        new Array(1, 2, 3)
        new Function('a, b', 'return a + b')
        new RegExp('\w', 'g')
      
        {}
        [1, 2, 3]
        function(a, b) { return a + b }
        /\w/g
      

Most have a protected constructor: new Array(1, 2, 3) OR Array(1, 2, 3) // [1, 2, 3]

[[Class]]

        const obj = {}
        const arr = []
        
      

Symbol.toStringTag

        const kottan = {
          [Symbol.toStringTag]: '๐Ÿˆโ€โฌ›'
        } 
        
      

What to Read โœ๏ธ

Questionsโ“

Type Conversions ๐Ÿงถ

Type Conversion

WTFJS

WTFKS dotJS 2012 - Brian Leroux - WTFJS

Kinds ๐Ÿฆ„

ToBoolean / Boolean(something)

ToString / String(something)

ToNumber / Number(something)

String To Number

        Number(' 14 ') // 14
        Number('14px') // NaN
        
      

ToPrimitive

  1. Symbol.toPrimitive

Symbol.toPrimitive

        const kottan = {
          
        }
      

Without Symbol.toPrimitive

        const kottan = {
          valueOf() { return '๐Ÿˆโ€โฌ›' },
          toString() { return {} }
        }
        
        
      

Implicit and Explicit ๐Ÿชก

ToBoolean

ToString

ToNumber

Comparisons ๐Ÿคผ

NaN

        const tricky = NaN
        tricky === NaN // false
        
        
        
      

Symbol

โ˜ ๏ธ

How to Avoid ๐Ÿชฆ

What to Read โœ๏ธ

Questionsโ“

Practice ๐Ÿง 

Practice ๐Ÿง 

Thanks ๐Ÿ‰