번역이 완료되지 않았습니다. Please help translate this article from English
typeof 연산자는 피연산자의 평가 전 자료형을 나타내는 문자열을 반환합니다.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
구문
typeof 연산자는 피연산자 앞에 위치합니다.
typef operand
매개변수
operand- 자료형을 가져올 객체 또는 원시값을 나타내는 표현식.
설명
typeof가 반환할 수 있는 값을 아래 표에서 볼 수 있습니다. 자료형과 원시값에 대한 자세한 정보는 JavaScript 자료형과 자료구조 페이지를 참고하세요.
| Type | Result |
|---|---|
| Undefined | "undefined" |
| Null | "object" (아래 참고) |
| Boolean | "boolean" |
| Number | "number" |
| BigInt | "bigint" |
| String | "string" |
| Symbol (ECMAScript 2015에서 추가) | "symbol" |
| 호스트 객체 (JS 환경에서 제공) | 구현체마다 다름 |
| Function 객체 (ECMA-262 표현으로는 [[Call]]을 구현하는 객체) | "function" |
| 다른 모든 객체 | "object" |
예제
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
typeof Number(1) === 'number'; // but never use this form!
typeof 42n === 'bigint';
// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof always returns a string
typeof String("abc") === 'string'; // but never use this form!
// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // but never use this form!
// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'
// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';
// Objects
typeof {a:1} === 'object';
// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [1, 2, 4] === 'object';
typeof new Date() === 'object';
// The following is confusing. Don't use!
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';
// Functions
typeof function(){} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';
추가 정보
null
// This stands since the beginning of JavaScript typeof null === 'object';
In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the bogus typeof return value. (reference)
A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === 'null'.
Regular expressions
Callable regular expressions were a non-standard addition in some browsers.
typeof /s/ === 'function'; // Chrome 1-12 Non-conform to ECMAScript 5.1 typeof /s/ === 'object'; // Firefox 5+ Conform to ECMAScript 5.1
명세
| Specification | Status | Comment |
|---|---|---|
| ECMAScript (ECMA-262) The definition of 'The typeof Operator' in that specification. |
Living Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'The typeof Operator' in that specification. |
Standard | |
| ECMAScript 5.1 (ECMA-262) The definition of 'The typeof Operator' in that specification. |
Standard | |
| ECMAScript 3rd Edition (ECMA-262) The definition of 'The typeof Operator' in that specification. |
Standard | |
| ECMAScript 1st Edition (ECMA-262) The definition of 'The typeof Operator' in that specification. |
Standard | Initial definition. Implemented in JavaScript 1.1. |
브라우저 호환성
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
typeof | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
Legend
- Full support
- Full support
IE 참고사항
On IE 6, 7, and 8 a lot of host objects are objects and not functions. For example:
typeof alert === 'object'