이 번역은 완료되지 않았습니다. 이 문서를 번역해 주세요.

typeof 연산자는 피연산자의 평가 전 자료형을 나타내는 문자열을 반환합니다.

구문

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 Latest Draft (ECMA-262)
The definition of 'The typeof Operator' in that specification.
Draft  
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.

브라우저 호환성

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
typeofChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes

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'

같이 보기

문서 태그 및 공헌자

이 페이지의 공헌자: alattalatta, mdnwebdocs-bot, dale0713
최종 변경자: alattalatta,