웹표준,웹접근성(html, html5, css, css3, javascript, jQuery, jQueryMobile, snecha, senchaTouch, php, mobileWebApp)

javascript 자료형 검사 typeof() 본문

javascript/Tutorial

javascript 자료형 검사 typeof()

jokack01 2013. 10. 30. 14:08

Data type 검사 

자바스크립트는 숫자, 문자, 불리언 같은 것을 자료형 이라고 부른다. 

자료형을 확인하려면 

typeof

연산자를 사용한다. 


ex ) 자료형 확인

<script>

//문자열

console.log(typeof ('문자열'));

//숫자

console.log(typeof (123));

//불리언

console.log(typeof (true));

//함수

console.log(typeof (function(){}));

//객체

console.log(typeof ({}));

//undefined

console.log(typeof (haha));

</script>


undefined는 정의하지 않은 자료형을 의미하는 것으로 

선언하지 않은 식별자 haha를 사용한 것이다. 


출력한 결과는 다음과 같다. 



ps. 그러나 typeof()는 설계 오류로 인해 null 값을 object 로 반환 시켜 준다.

그래서.. jQuery의 type()메서드나 Constuctor 를 쓰는 것이 좋다.


'javascript > Tutorial' 카테고리의 다른 글

array(배열)  (1) 2013.11.04
jascript prompt(입력), confirm(확인)  (0) 2013.10.30
javascript 연산자  (0) 2013.10.30
javascript data type  (0) 2013.10.25
javascript variable (변수)  (0) 2013.10.25