1 - 3 Variable types

👩‍💻NEO_매트릭스·2022년 1월 21일
0

JAVA

목록 보기
3/11
post-thumbnail

1 - 3 Variable types

자바스크립트 데이터 타입들에 대해서 알아보자.

  • primitive type - 더이상 작은 단위로 나누어질 수 없는 한가지의 아이템
    ⇒ number, string, boolean, null, undefined, symbol
  • object, box container -
    싱글아이템을 여러개를 묶어서 한 박스로 관리할 수 있도록 해줌.
    ⇒ function, first-class function
    const count = 17; *// integer 정수*
    const size = 17.1; *// decimal number 소수점의 숫자*
    console.log(`value: ${count}, type: ${typeof count}`);
    console.log(`value: ${size}, type: ${typeof size}`);
    
    // number - speicla numeric values: infinity, -infinity, NaN
    // 계산할때 나누고자하는 값을 정확하게 확인하지 않고 연산을 하면 에러가 발생
    // 현업에서 이런 문제로 에러가 발생*
    
    const infinity = 1 / 0; *//숫자를 0으로 나누면 무한대의 숫자가 발생*
    const negativeInfinity = -1 / 0; *// 네거티브*
    const nAn = 'not a number' / 2; *//숫자가 아닌 문자를 나누면 NaN값이 출력*
    console.log(infinity);
    console.log(negativeInfinity);
    console.log(nAn)  
   

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

숫자 마지막에 n을 붙이면 큰 숫자로 확인하는 최신 버젼 추가

const bigInt = 123456789012345678901234567890n;
// over (-2*53) ~ 253)
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}`);
    
     //template literals (string) 변수값을 붙일 수 있다.
    const helloBob = `hi ${brendan}!`;
    console.log(`value: ${helloBob}, type: ${typeof helloBob}`);
    
    //이렇게 간편하게 출력가능
    console.log('value: ' + helloBob + ' type: ' + typeof helloBob);
  • 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}`);
    console.log(`value: ${test}, type: ${typeof test}`);
  • null

    let nothing = null;
    console.log(`value: ${nothing}, type: ${typeof nothing}`);
  • undefined 선언은 되었지만 값이 지정되어 있지 않은 상태
    let x;  
    console.log(`value: ${x}, type; ${typeof x}`);
  • symbol, create unique identifiers for objects

    ⇒ 고유한 식별자를 만들때 둘은 같지 않다.

    const symbol1 = Symbol('id'); 
    const symbol2 = Symbol('id');
    console.log(symbol1 === symbol2); //false
    
    // 스트링이 똑같다면 for을 사용해서 같게 만들어달라고 요청가능
    const gSymbol1 = Symbol.for('id');
    const gSymbol2 = Symbol.for('id');
    console.log(gSymbol1 === gSymbol2); //true 
    
    // 출력시 .description사용하여 스트링으로 변환해서 출력해야함
    console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);
  • object, real-life object, data structure
    ⇒ neo는 편경불가
    name과 , age는 변경이 가능한 변수이기때문에

    const neo = {name: 'neo', age: 100};
    neo.age = 20; //변경이 가능

0개의 댓글