typeof
typeof
typeof
操作符返回一个字符串,指示未经计算的操作数的类型。
语法
该typeof
操作是紧跟其操作:
typeof operand
参数
operand
是一个表达式,表示对象或原始值,其类型将被返回。
描述
下表总结了typeof可能的返回值。有关类型和原始值的更多信息,可查看 JavaScript数据结构 页面。
Type | Result |
---|---|
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" |
示例
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === '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 '1' === 'string'; // note that a number within a string is still typeof 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';
在 JavaScript 最初的实现中,JavaScript 中的值是由一个表示类型的标签和实际数据值表示的。对象的类型标签是 0。由于 null
代表的是空指针(大多数平台下值为 0x00),因此,null
的类型标签也成为了 0,typeof null
就错误的返回了"object"
。(reference)
ECMAScript提出了一个修复(通过opt-in),但被拒绝。这将导致typeof null === 'object'。
使用 new 操作符
// All constructor function while instaitated with 'new' keyword will always be typeof 'object'
var str = new String('String'
var num = new Number(100
typeof str; // It will return 'object'
typeof num; // It will return 'object'
//But there is a exception in case of Function constructor of Javascript
var func = new Function(
typeof func; // It will return 'function'
正则表达式
对正则表达式字面量的类型判断在某些浏览器中不符合标准:
typeof /s/ === 'function'; // Chrome 1-12 Non-conform to ECMAScript 5.1
typeof /s/ === 'object'; // Firefox 5+ Conform to ECMAScript 5.1
例外
所有当前的浏览器都暴露了一个类型为 undefined 的非标准宿主对象 document.all。
typeof document.all === 'undefined';
尽管规范允许为非标准的外来对象定制类型标签,但它要求这些类型标签与预定义标签不同。document.all
的类型标记为“undefined
”的情况必须被列为违反规则的特殊情况。
规范
Specification | Status | Comment |
---|---|---|
ECMAScript Latest Draft (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. |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |