JavaScript 之 typeof
typeof
操作符返回一个字符串,表示未经计算的操作数的类型。
JavaScript Demo: Expressions - typeof
console.log(typeof 42); // expected output: "number" console.log(typeof 'blubber'); // expected output: "string" console.log(typeof true); // expected output: "boolean" console.log(typeof declaredButUndefinedVariable); // expected output: "undefined";
语法
typeof
运算符后跟操作数:
typeof operand or typeof (operand)
参数
operand
是一个表达式,表示对象或原始值,其类型将被返回。
括号是可选的。
描述
下表总结了typeof
可能的返回值。有关类型和原始值的更多信息,可查看 JavaScript数据结构 页面。
类型 | 结果 |
---|---|
Undefined |
"undefined"
|
Null |
"object" (见下文)
|
Boolean |
"boolean"
|
Number |
"number"
|
String |
"string"
|
Symbol (ECMAScript 6 新增) |
"symbol"
|
宿主对象(由JS环境提供) | Implementation-dependent |
函数对象([[Call]] 在ECMA-262条款中实现了) |
"function"
|
任何其他对象 |
"object"
|
示例
1 // Numbers 2 typeof 37 === 'number'; 3 typeof 3.14 === 'number'; 4 typeof Math.LN2 === 'number'; 5 typeof Infinity === 'number'; 6 typeof NaN === 'number'; // 尽管NaN是"Not-A-Number"的缩写 7 typeof Number(1) === 'number'; // 但不要使用这种形式! 8 9 // Strings 10 typeof "" === 'string'; 11 typeof "bla" === 'string'; 12 typeof (typeof 1) === 'string'; // typeof总是返回一个字符串 13 typeof String("abc") === 'string'; // 但不要使用这种形式! 14 15 // Booleans 16 typeof true === 'boolean'; 17 typeof false === 'boolean'; 18 typeof Boolean(true) === 'boolean'; // 但不要使用这种形式! 19 20 // Symbols 21 typeof Symbol() === 'symbol'; 22 typeof Symbol('foo') === 'symbol'; 23 typeof Symbol.iterator === 'symbol'; 24 25 // Undefined 26 typeof undefined === 'undefined'; 27 typeof declaredButUndefinedVariable === 'undefined'; 28 typeof undeclaredVariable === 'undefined'; 29 30 // Objects 31 typeof {a:1} === 'object'; 32 33 // 使用Array.isArray 或者 Object.prototype.toString.call 34 // 区分数组,普通对象 35 typeof [1, 2, 4] === 'object'; 36 37 typeof new Date() === 'object'; 38 39 // 下面的容易令人迷惑,不要使用! 40 typeof new Boolean(true) === 'object'; 41 typeof new Number(1) === 'object'; 42 typeof new String("abc") === 'object'; 43 44 // 函数 45 typeof function(){} === 'function'; 46 typeof class C{} === 'function' 47 typeof Math.sin === 'function'; 48 typeof new Function() === 'function';
null
typeof null === 'object'; // 从一开始出现JavaScript就是这样的
在 JavaScript 最初的实现中,JavaScript 中的值是由一个表示类型的标签和实际数据值表示的。对象的类型标签是 0。由于 null
代表的是空指针(大多数平台下值为 0x00),因此,null的类型标签也成为了 0,typeof null
就错误的返回了"object"
。(reference)
ECMAScript提出了一个修复(通过opt-in),但被拒绝。这将导致typeof null === 'object'。
使用 new
操作符
// All constructor functions while instantiated 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'
语法中需要括号
// Parentheses will be very much useful to determine the data type for expressions. var iData = 99;typeof iData + ' Wisen'; // It will return 'number Wisen' typeof (iData + ' Wisen'); // It will return 'string'
正则表达式
对正则表达式字面量的类型判断在某些浏览器中不符合标准:
typeof /s/ === 'function'; // Chrome 1-12 , 不符合 ECMAScript 5.1 typeof /s/ === 'object'; // Firefox 5+ , 符合 ECMAScript 5.1
暂存死区
typeof undeclaredVariable === 'undefined'; typeof newLetVariable; let newLetVariable; // ReferenceError typeof newConstVariable; const newConstVariable = 'hello'; // ReferenceError
例外
所有当前的浏览器都暴露了一个类型为 undefined 的非标准宿主对象 document.all
。
typeof document.all === 'undefined';
参照来源:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof
转载于:https://www.cnblogs.com/xianfengzhike/p/9168997.html
JavaScript 之 typeof相关推荐
- JavaScript中typeof的用法
JavaScript中typeof知多少? typeof运算符介 绍: typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型. 它返回值是一个字符串,该字符串说明运算数的类型. 你 ...
- 关于JavaScript中typeof的用法
一.typeof的作用 在JavaScript中,变量未经声明就使用,系统是会报错的.但是,typeof却是js中有且仅有的一个特例. typeof的作用就是用来区分数据类型的,下面先说说typeof ...
- JavaScript数据类型 typeof, null, 和 undefined
JavaScript 数据类型 在 JavaScript 中有 5 种不同的数据类型: string number boolean object function 3 种对象类型: Object Da ...
- javascript : instanceof,typeof,constructor
数据类型 javascript中包含6种数据类型:undefined.null.string.number.boolean和object.其中,前5 种是原始数据类型,object是对象类型. obj ...
- javascript的typeof返回哪些数据类型
1.返回数据类型 undefined string boolean number symbol(ES6) Object Function 2.强制类型转换 Number(参数)把任何类型转换成数值类型 ...
- Javascript中typeof instanceof constructor的区别
typeof typeof,是一个运算符,运算中需要一个操作数,运算的结果就是这个操作数的类型,运算的结果是一个字符串.他有一定的局限性,对于对象类型的值,只能得到一个object结果,却不能精确得到 ...
- JavaScript 用typeof来判断是否是对象的弊端
题目:使用 typeof bar === "object" 来确定 bar 是否是对象的潜在陷阱是什么?如何避免这个陷阱? 1:使用typeof 首先看下面的代码: var car ...
- 前端学习(1692):前端系列javascript之typeof
- 扩展typeof来判断js变量的类型
Javascript 的 typeof可以获取变量的类型,有如下6种返回值: 1)number: 2)string: 3)boolean: 4)object 5)function: 6)u ...
最新文章
- DSP与CEVA芯片
- JAVA基础13-Java抽象方法、抽象类、抽象之模板方法模式
- JavaScript在离开页面是提示用户
- 2006年大话IT数码新潮
- ubuntu安装VMware Tools
- 魔兽三国服务器维护,魔兽三国开服七天技巧
- c语言pwm调制方式,pwm调制原理同步调制_几种pwm调制方式介绍 - 全文
- php 自动验证类,Thinkphp实现自动验证和自动完成
- 14-项目开发总结报告(GB8567——88)
- 表格制作模块xlwt
- Android模拟地图gps定位
- ★★iPhone越狱后必装软件和一些心得教程,送给广大的小白们,希望你们能从小白变成“小黑”★★
- 统计学 —— 单因素方差分析的应用与Excel实现
- Win10 + Ubuntu 安装教程(痛苦踩坑)
- 软件著作权申请表怎么填
- 使用OpenCV将一个三角形图形扭曲到另一个三角形
- 研发主管接私活被辞退,法院判决公司赔偿20.7万元
- C++加载csb文件
- UVA 11134 Fabled Rook 枚举 multiset加速
- 刷题之旅第35站,CTF show 萌新题目集合