본문 바로가기

JavaScript

[JavaScript] 자료형 검사(typeof 연산자)

반응형

자료형 검사(typeof 연산자)


 자바스크립트는 숫자, 문자열, 불리언 같은 것을 자료형(자료의 형태)이라고 부른다. 자료형을 확인하려면 typeof 연산자를 사용해야 한다.


 ex01.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>연습문제</title>
    <script type="text/JavaScript">
        alert(typeof('Strig'));
        alert(typeof('273'));
    </script>
</head>
</html>

 출력결과



 typeof 연산자를 사용해 모든 자료형을 살펴보자.


 ex02.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>연습문제</title>
    <script type="text/JavaScript">
        // 문자열
        alert(typeof('String'));

        // 숫자
        alert(typeof('273'));

        // 불리언
        alert(typeof(true));

        // 함수
        alert(typeof(function() {}));

        // 객체
        alert(typeof({}));
    </script>
</head>
</html>

 출력결과


반응형