티스토리 뷰

Javascript

Javascript - 자료형

김소비요정 2021. 8. 28. 12:20

Variable types

메모리의 값이 저장되는 방식에 따라 두가지 type으로 나눌 수 있다.

  1. privimitive type
  2. object type

privimitive type

  • single item ( → 더 이상 나누어질 수 없는 한 가지의 타입을 의미)
  • 값 자체가 메모리에 저장
  • number, string, boolean, null, undefined, symbol
  • JavaScript는 조건절, 반복문 등 Boolean 값이 필요한 곳에서 형 변환을 이용해 특정 값을 Boolean 값으로 변환함
    ** Falsy(거짓 같은 값) : 0, null, undefined, NaN, ''
    ** Truthy(참 같은 값) : any other value
// 1. number
const count = 17; // integer
const size = 17.1; // decimal number
console.log(`value: ${count}, type: ${typeof count}`);
console.log(`value: ${size}, type: ${typeof size}`);

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

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

// 3. boolean
// true, false
// ** JavaScript는 조건절, 반복문 등 Boolean 값이 필요한 곳에서 형 변환을 이용해 특정 값을 Boolean 값으로 변환함
// ** Falsy(거짓 같은 값) : 0, null, undefined, NaN, ''
// ** Truthy(참 같은 값) : 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}`);

// 4. null
// 내가 너는 비어있는 값이야라고 지정해 주는 것
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);

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

// 6. symbol
// 고유한 식별자가 필요할때 쓰여진다.
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(gSymbol2.description);

Object type

  • box container ( → 위 single item들을 묶어 하나의 단위로, 박스로 관리할 수 있는 타입을 의미
  • 메모리가 담겨있는 곳을 가르키는 값이 저장되어 있음
const amy = { name: 'amy', age: 20 };
amy.age = 30;

https://www.youtube.com/watch?v=OCCpGh4ujb8&list=PLv2d7VI9OotTVOL4QmPfvJWPJvkmv6h-2&index=3 

 

'Javascript' 카테고리의 다른 글

Javascript - Callback function, First-Class function  (0) 2021.09.01
Javascript - function, parameter  (0) 2021.09.01
Javascript - 논리연산자  (0) 2021.08.28
Javascript - 변수 선언  (0) 2021.08.27
JavaScript(ES5+) - defer / use strict  (0) 2021.08.27