ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2. data types, let vs var, hoisting
    JavaScript & TypeScript 2020. 11. 13. 17:21

     

    1. let vs var, hoisting

    1. Variable, rw(read/write)
    2. let (added in ES6)

     

    • scope
      • global scope
      • block scope

     

    Var

    • var (don't ever ues this)
    • var hoisting (move declaration from bottom to top)
    • has no block scope
    • block scope ignore

     

    Constant, r (read only)

    •  favor immutable data type always for a few reasons 
      • security
      • thread safety
      • reduce human mistakes

     


    2. data types

     

    Variable types

    • primitive, single item
      • number
      • string
      • boolean
      • null
      • undefined
      • symbol

     

    • object
    • box container
    • function
    • first-class function

     

    | Note !

    • immutable data types 
      • primitive types
      • frozen objects (i.e.object.freeze())

     

    • mutable data types
      • all objects by default are mutable in JS

     

    number

    const count = 17; // integer
    const size = 17.1; // decimal number
    console.log(`value: ${count}, type: ${typeof count}`); // 17, number
    console.log(`value: ${size}, type: ${typeof size}`); // 17.1 number

     

    - number - special numeric values : infinity, -infinity, NaN

    const infinity = 1 / 0;
    const negativeInfinity = -1 / 0;
    const nAn = 'not a number' / 2;
    console.log(infinity); // Infinity
    console.log(negativeInfinity); // -Infinity
    console.log(nAn); // NaN

     

    - bigInt (fairly new, don't use it yet)

    const bigInt = 123456678012345667891234566789123456767890n; // over (-2*53) ~ 2*53
    console.log(`value: ${bigInt}, type: ${typeof bigInt}`);
    Number.MAX_SAFE_INTEGER;

     

     

    string

    const char = 'c';
    const brendan = 'brendan';
    const greeting = 'hello ' + brendan;
    console.log(`value: ${greeting}, type: ${typeof greeting}`); // string
    const helloBob = `hi ${brendan}!`; // template literals (string)
    console.log(`value: ${helloBob}, type: ${typeof helloBob}`); // string

     

     

    boolean

    • false
      • 0
      • null
      • undefined
      • NaN
      • ""

     

    • true
      • any other value
    const canRead = true;
    const test = 3 < 1; // false
    console.log(`value: ${canRead}, type: ${typeof canRead}`); // boolean
    console.log(`value: ${test }, type: ${typeof test }`); // boolean

     

     

    null

    let nothing = null;
    console.log(`value: ${nothing}, type: ${typeof nothing}`); // null

     

     

    undefined

    let x;
    console.log(`value: ${x}, type: ${typeof x}`); // undefined

     

     

    symbol

    - symbol, create unique identifiers for objects

    const symbol1 = Symbol('id');
    const symbol2 = Symbol('id');
    console.log(symbol1 === symbol2) // false
    const gSymbol1 = Symbol.for('id');
    const gSymbol2 = Symbol.for('id');
    console.log(gSymbol1 === gSymbol2 ) // true
    console.log(`value: ${symbol1.description}`); // id

     

     

    object, real-life object, data structure

    const ellie = { name: 'ellie', age: 20 };
    ellie.age = 21;

     

     

     


     

    Dynamic typing : dynamically typed language

    let text = 'hello';
    console.log(text.charAt(0)); // h
    console.log(`value: ${text}, type: ${typeof text}`); // hello, string
    text = 1;
    console.log(`value: ${text}, type: ${typeof text}`); // 1, number
    text = '7' + 5;
    console.log(`value: ${text}, type: ${typeof text}`); // 75, string
    text = '8' / '2';
    console.log(`value: ${text}, type: ${typeof text}`); // 4, number

     

     

     

     

     


    ※출처

    www.youtube.com/channel/UC_4u-bXaba7yrRz_6x6kb_w

    'JavaScript & TypeScript' 카테고리의 다른 글

    6. what is object  (0) 2020.11.14
    5. class vs object  (0) 2020.11.14
    4. Arrow Function  (0) 2020.11.13
    3. operator, if, for loop  (0) 2020.11.13
    1. script async vs defer  (0) 2020.11.13
킹수빈닷컴