프로그래밍/JavaScript

[자바스크립트] Data types, let vs var, hoisting

choar 2021. 3. 21. 19:29
반응형

드림코딩 엘리 - 자바스크립트 강의 #3

 

1. Use strict

  • added in ES5
  • use this for Vanilla JavaScript
  • ex) 'use strict';

2. Variable, rw(read/write)

  • let (added in ES6)
  • var (don't ever use this!)
    • var hoisting (move declaration from bottom to top) : 선언하기도 전에 할당할 수 있음
    • has no block scope : (), {} 뚫고 나옴

3. Constant, read(read only)

  • use const whenever possible.
  • only use let if variable needs to change.
  • ex) const daysInWeeks = 7;

 

  • Note!
  • Immutable data types: primitive types, frozen objects (i.e. object.freeze())
  • Mutable data types: all objects by default are mutable in JS
  • favor immutable data type always for a few reasons:
    • security
    • thread safety
    • reduce human mistakes

 

4. Variable types

  • primitive, single item (더이상 쪼갤 수 x 단위) : number, string, boolean, null, undefined, symbol
  • object, box container
  • function, first-class function
  • data types for number
    • C : short, int, long, float, double
    • Java : byte, short, long, int, float, double
    • JS : number
      • ex) let a = 12; let b = 1.2;
    • TS : number
      • ex) let a: number = 12;
  • number - special numeric values : infinity, -infinity, NaN
const infinity = 1/0;
const negativeInfinity = -1/0;
const nAn = 'not a number'/2;
  • bigInt : 범위 초과하는 수 뒤에 n만 붙이면 bigInt 타입으로 인식. 크롬, 파이어폭스에서만 됨 (fairly new)
  • string
  • boolean
  • null
  • undefined
  • symbol

 

5. Dynamic typing: dynamically typed language

let text = 'hello';
console.log(`value: ${text}, type: ${typeof text}`);
//⇒ value: hello, type: string

text = 1;
console.log(`value: ${text}, type: ${typeof text}`);
//⇒ value: 1, type: number

text = '7' + 5;
console.log(`value: ${text}, type: ${typeof text}`);
//⇒ value: 75, type: string

text = '8' / '2';
console.log(`value: ${text}, type: ${typeof text}`);
//⇒ value: 4, type: number
반응형