[javascript]typeof 연산자 활용방법

최준호·2022년 8월 22일
0

typeof 연산자

typeof는 변수의 데이터 타입을 반환해주는 연산자이다.

문법

typeof hello
typeof(hello)
  • hello에는 데이터 또는 변수가 들어간다. 괄호를 사용해도 된다.

typeof

반환되는 값은 아래와 같다.

  • undefined : 변수가 정의되지 않거나 값이 없을 때
  • number : 데이터 타입이 수일 때
  • string : 데이터 타입이 문자열일 때
  • object : 데이터 타입이 함수, 배열 등 객체일 때
  • function : 변수의 값이 함수일 때
  • symbol : 데이터 타입이 심볼일 때

typeof 사용법

표현식반환값
typeof undefined"undefined"
typeof null"object"
typeof true"boolean"
typeof 1"number"
typeof 1"string"
typeof 1"symbol"
typeof 1"function"
typeof 1"object"

<예시>

console.log(typeof undeclaredVariable);
// 출력 결과: "undefined"

console.log(typeof null);
// 출력 결과: "object"

console.log(typeof true);
// 출력 결과: "boolean"

console.log(typeof 27);
// 출력 결과: "number"

console.log(typeof 'YD');
// 출력 결과: "string"

console.log(typeof Symbol());
// 출력 결과: "symbol"

console.log(typeof function() {});
// 출력 결과: "function"

console.log(typeof {});
// 출력 결과: "object"
profile
LV2 프론트엔드 엔지니어

0개의 댓글