Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

typeof

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

typeof 연산자는 피연산자의 타입을 가리키는 문자열을 반환합니다.

문법

typeof 연산자는 다음에 피연산자가 옵니다 : 

typeof피연산자

파라미터

피연산자는 타입을 반환할 오브젝트나 기본형(primitive)을 나타내는 표현식입니다.

설명

다음 표는 반환 가능한 타입 값들을 요약하고 있습니다. 타입과 기본형에 대한 더 많은 정보에 대해서는 자바스크립트 데이터 구조(JavaScript data structure) 페이지를 보세요.

타입 결과
Undefined "undefined"
Null "object" (see below)
Boolean "boolean"
Number "number"
String "string"
Symbol (new in ECMAScript 2015) "symbol"
Host object (provided by the JS environment) Implementation-dependent
Function object (implements [[Call]] in ECMA-262 terms) "function"
Any other object "object"

 

Examples

// 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!


// 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

Specifications

Specification Status Comment
ECMAScript 2017 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.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

IE-specific notes

On IE 6, 7, and 8 a lot of host objects are objects and not functions. For example:

typeof alert === 'object'

See also

문서 태그 및 공헌자

 이 페이지의 공헌자: dale0713
 최종 변경: dale0713,