typeof 연산자
typeof는 변수의 데이터 타입을 반환해주는 연산자이다.
문법
typeof hello
typeof(hello)
typeof
반환되는 값은 아래와 같다.
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"