typeof 는 js에서 타입을 반환하는 연산자(operator)로써 변수의 타입을 확인할 때 사용한다.
typeof 로 아래와 같이 number, string, object, boolean, function 타입을 확인 할 수 있다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
 | 
 console.log(typeof 123);              //number 
console.log(typeof "str");             //string 
console.log(typeof new Object());      //object 
console.log(typeof {test : "test"});   //object 
console.log(typeof true);              //boolean 
console.log(typeof Symbol());          //symbol 
console.log(typeof function(){});      //function 
console.log(typeof 123 === 'number');               //true 
console.log(typeof "str" === 'string');             //true 
console.log(typeof new Object() === 'object');      //true 
console.log(typeof {test : "test"} === 'object');   //true 
console.log(typeof true === 'boolean');             //true 
console.log(typeof Symbol() === 'symbol');          //true 
console.log(typeof function(){} === 'function');    //true 
 | 
cs | 
다음과 같은 타입은 주의하여 사용하자.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
 | 
 console.log(typeof null);                    //object 
console.log(typeof new Object(123));        //object 
console.log(typeof new Object("123"));        //object 
console.log(typeof null === 'undefined');                //false 
console.log(typeof new Object(123) === 'object');        //true 
console.log(typeof new Object(123) === 'number');        //false 
console.log(typeof new Object("123") === 'string');        //false 
 | 
cs | 
1. null 은 object (null 은 != null 과 같이 체크 할 것)
2. Object에 담긴 number/string 는 object
undefined : 선언은 되어있으나 아무 값도 저장되어 있지 않은 경우
undeclared : 변수 선언 자체가 되어 있지 않은 경우
* undefined , undeclared 모두 typeof 시 undefined로 반환
참고 :
반응형
    
    
    
  'front > javascript' 카테고리의 다른 글
| [javascript] 자바스크립트의 Scope 정리 (0) | 2019.11.12 | 
|---|---|
| [javascript] 호이스팅(hoisting)이란? (0) | 2019.11.11 | 
| [javascript] 함수의 특징 (1급 객체) (0) | 2019.11.09 | 
| 페이지를 벗어나는 경우 이벤트 처리 : onbeforeunload (0) | 2019.11.01 | 
| form 태그 file 명 바꿔주는 법 + UUID (0) | 2019.11.01 |