타입 체크

typeof

typeof 연산자는 피연산자의 데이터 타입을 문자열로 반환한다.

typeof '';              // string
typeof 1;               // number
typeof NaN;             // number
typeof true;            // boolean
typeof [];              // object
typeof {};              // object
typeof new String();    // object
typeof new Date();      // object
typeof /test/gi;        // object
typeof function () {};  // function
typeof undefined;       // undefined
typeof null;            // object (설계적 결함)
typeof undeclared;      // undefined (설계적 결함)

typeof는 객체의 종류까지 구분하여 체크하려할 때는 사용하기가 곤란하다.

toString

좋은 웹페이지 즐겨찾기