자바스크립트 에서의 타입(Type) ?

typeOf

먼저, 자바스크립트 언어의 타입은 원시 값과 객체로 나뉜다.

자바스크립트는 7 가지의 원시값 타입을 가지는데

  • Boolean
  • Null
  • Undefined
  • Number
  • BigInt
  • String
  • Symbol

그리고 참조값 타입 객체(Object), 객체의 하위 타입 ( Map, WeakMap, Set WeakSe,함수(function)t 등)이 있다

자바스크립트에서는 typeof 라는 연산자를 사용해서 변수의 데이터 타입을 반환한다.

원시값 타입

const a = 123;

console.log(typeof a);

// return 'number'

const b = 'string';

console.log(typeof b);

// return 'string'

const c = true;

console.log(typeof c);

// return 'boolean'

const d = Symbol('aa');

console.log(typeof d)

// return 'symbol'

const obj = {a : 1};

console.log(typeof obj);

// return 'object'

const undef = undefined;

console.log(typeof undef);

// return 'undefined'

객체

const array = [];

console.log(typeof array);

// return 'object'

 //객체가 배열인지 확인하기 위해서는 isArray() 라는 내장함수를 사용해 확인한다.

const func = function(){};

console.log(typeof func);

// return 'function'
	// function 타입은 일급객체(first-class function 로 취급

const empty = null;

console.log(typeof empty);

// return 'object'

	// 여기서 null 타입은 ‘object’ 타입 이라기 보다는 'null' 이라는 
	// 하나의 원시타입(primitive type)으로 정의 되어야 맞지만, 
	// 이미 많은 코드 들이 `typeof null === ‘object` 를 반환하는 코드를 사용하기 때문에 
	// 실질적으로 바꾸기는 어려운 상황이라고 한다.

0개의 댓글