typeof 연산자는 단일 피연산자 앞에 배치되는 단항 연산자로, 모든 유형이 될 수 있습니다. 그 값은 피연산자의 데이터 유형을 나타내는 문자열입니다. typeof 연산자는 피연산자가 숫자, 문자열 또는 부울 값인 경우 "숫자", "문자열" 또는 "부울"로 평가하고 평가에 따라 true 또는 false를 반환합니다.
다음은 typeof의 반환 값 목록입니다. 오퍼레이터.
| 유형 | typeof에서 반환되는 문자열 |
|---|---|
| 숫자 | "숫자" |
| 문자열 | "문자열" |
| 부울 | "부울" |
| 객체 | "객체" |
| 함수 | "기능" |
| 정의되지 않음 | "정의되지 않음" |
| 널 | "객체" |
예시
다음 코드는 typeof를 구현하는 방법을 보여줍니다. 연산자 -
<html>
<body>
<script>
var a = 10;
var b = "String";
var linebreak = "<br />";
result = (typeof b == "string" ? "B is String" : "Bis Numeric");
document.write("Result => ");
document.write(result);
document.write(linebreak);
result = (typeof a == "string" ? "A is String" : "Ais Numeric");
document.write("Result => ");
document.write(result);
document.write(linebreak);
</script>
</body>
</html>