typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well.

typeof是一个JavaScript关键字,当您调用它时将返回变量的类型。 您可以使用它来验证函数参数或检查是否定义了变量。 还有其他用途。

The typeof operator is useful because it is an easy way to check the type of a variable in your code. This is important because JavaScript is a is a dynamically typed language. This means that you aren’t required to assign types to variables when you create them. Because a variable is not restricted in this way, its type can change during the runtime of a program.

typeof运算符很有用,因为它是检查代码中变量类型的简便方法。 这很重要,因为JavaScript是一种动态类型的语言 。 这意味着在创建变量时不需要为变量分配类型。 因为不以此方式限制变量,所以其类型可以在程序运行时更改。

For example:

例如:

var x = 12345; // number
x = 'string'; // string
x = { key: 'value' }; // object

As you can see from the above example, a variable in JavaScript can change types throughout the execution of a program. This can be hard to keep track of as a programmer, and this is where the typeof operator is useful.

从上面的示例可以看到,JavaScript中的变量可以在程序执行期间更改类型。 作为程序员可能很难跟踪,这就是typeof运算符有用的地方。

The typeof operator returns a string that represents the current type of a variable. You use it by typing typeof(variable) or typeof variable. Going back to the previous example, you can use it to check the type of the variable x at each stage:

typeof运算符返回一个表示变量当前类型的字符串。 您可以通过键入typeof(variable)typeof variable来使用它。 回到上一个示例,您可以在每个阶段使用它来检查变量x的类型:

var x = 12345;
console.log(typeof x) // number
x = 'string';
console.log(typeof x) // string
x = { key: 'value' };
console.log(typeof x) // object

This can be useful for checking the type of a variable in a function and continuing as appropriate.

这对于检查函数中变量的类型并酌情继续操作很有用。

Here’s an example of a function that can take a variable that is a string or a number:

这是一个函数示例,该函数可以采用字符串或数字作为变量:

function doSomething(x) {if(typeof(x) === 'string') {alert('x is a string')} else if(typeof(x) === 'number') {alert('x is a number')}
}

Another way the typeof operator can be useful is by ensuring that a variable is defined before you try to access it in your code. This can help prevent errors in a program that may occur if you try to access a variable that is not defined.

typeof运算符有用的另一种方式是,在尝试在代码中访问变量之前,确保已定义了变量。 如果您尝试访问未定义的变量,这可以帮助防止程序中可能发生的错误。

function(x){if (typeof(x) === 'undefined') {console.log('variable x is not defined');return;}// continue with function here...
}

The output of the typeof operator might not always be what you expect when you check for a number.Numbers can turn in to the value NaN (Not A Number) for multiple reasons.

当检查数字时, typeof运算符的输出可能并不总是您期望的。 数字可能由于多种原因而变成值NaN(非数字) 。

console.log(typeof NaN); //"number"

Maybe you tried to multiply a number with an object because you forgot to access the number inside the object.

也许您试图将一个对象与一个数字相乘,因为您忘记了访问该对象内部的数字。

var x = 1;
var y = { number: 2 };
console.log(x * y); // NaN
console.log(typeof (x * y)); // number

When checking for a number, it is not sufficient to check the output of typeof for a number, since NaN alsopasses this test.This function check for numbers, and also doesn’t allow the NaN value to pass.

当一个号码的检查,这是不够的,检查的输出typeof的数量,因为NaN alsopasses的人数这个test.This功能检查,也不允许NaN值传递。

function isNumber(data) {return (typeof data === 'number' && !isNan(data));
}

Even thought this is a useful validation method, we have to be careful because javascript has some weird parts and one of them is the result of typeof over particular instructions. For example, in javascript many things are just objects so you’ll find.

即使认为这是一种有用的验证方法,我们也要小心,因为javascript有一些奇怪的部分,其中之一是typeof over特定指令的结果。 例如,在javascript中,很多东西都只是objects所以您会发现。

var x = [1,2,3,4];
console.log(typeof x)  // objectconsole.log(typeof null)  // object

更多信息: (More Information:)

MDN Documentation for typeof

有关typeof的MDN文档

翻译自: https://www.freecodecamp.org/news/javascript-data-types-typeof-explained/

JavaScript数据类型:Typeof解释相关推荐

  1. JavaScript数据类型 typeof, null, 和 undefined

    JavaScript 数据类型 在 JavaScript 中有 5 种不同的数据类型: string number boolean object function 3 种对象类型: Object Da ...

  2. javaScript数据类型 转换方式

    大家好,我是瓶盖袁 !今天给大家介绍一下javaScript数据类型以及的转换方式,废话不多说我们步入正题. 数据类型 ECMAScript中有五种数据Number 数值,String 字符串,Boo ...

  3. 在CodeMash 2012的“ Wat”演讲中提到的这些怪异JavaScript行为的解释是什么?

    本文翻译自:What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk fo ...

  4. 18100出多少取整_关于JavaScript数据类型,你知道多少?

    JavaScript的数据类型是每一个前端开发者必须要掌握的内容,也是最基础最重要的角色之一,关于JavaScript数据类型你知道多少? JavaScript一共有7种数据类型:String.Num ...

  5. JavaScript筑基篇(二)-JavaScript数据类型

    说明 介绍JavaScript数据类型 目录 前言 参考来源 前置技术要求 JavaScript的6种数据类型 哪6种数据类型 undefined 类型 null 类型 boolean 类型 numb ...

  6. Javascript数据类型共有六种

    Javascript数据类型共有六种 /* var box; alert(typeof box); // box是Undefined类型,值是undefined,类型返回的字符串是undefinedv ...

  7. 关于JavaScript中typeof的用法

    一.typeof的作用 在JavaScript中,变量未经声明就使用,系统是会报错的.但是,typeof却是js中有且仅有的一个特例. typeof的作用就是用来区分数据类型的,下面先说说typeof ...

  8. javascript 数据类型与类型转换

    JavaScript 数据类型 值类型(基本类型): 字符串(String) 数字(Number) 布尔(Boolean) 空(Null) 未定义(Undefined) Symbol 注:Symbol ...

  9. JavaScript数据类型基本数据类型与引用数据类型的区别

    JavaScript数据类型 1.JavaScript判断数据类型 typeof操作符 使用typeof操作符可以返回变量的数据类型 typeof操作符有带括号和不带括号两种用法 typeof (变量 ...

  10. JavaScript 数据类型之 Symbol、BigInt

    JavaScript数据类型 在JavaScript中,我们已知有5种基本数据类型:Undefined.Null.String.Number.Boolean. 当ES6问世,直至今日,又新增了两种基本 ...

最新文章

  1. Ansible — ansible.cfg 配置文件解析
  2. 常用排序算法之——堆排序
  3. SpringCloud实践分享-日志收集Kafka-ELK
  4. boost::process::start_dir相关的测试程序
  5. Insyde uefi 隐藏设置_安卓福利,手机桌面图标隐藏,找应用按字母轻松搞定!
  6. mysql加索引快很多
  7. 教你webpack、react和node.js环境配置(上篇)
  8. sql通用防注入程序php,sql通用防注入系统_PHP教程
  9. python学习之网络编程
  10. 2016 Multi-University Training Contest 10
  11. 3S基础知识:MapX应用教程—查询
  12. 添加三个字母即可免费下载百度文库的文档
  13. 上下文无关文法的组成
  14. EfficientDet: Scalable and Efficient Object Detection
  15. 六、肿瘤RNA突变的全组学研究-肿瘤基因调控(Genomic basis for RNA alterations in cancer)
  16. ARM中的浮点运算测试
  17. Centos7安装gos脚本
  18. IOS VLC 播放器 开发 滑动快进和后退
  19. 基于kinect的人体动作识别系统
  20. 【C语言练习——打印空心三角形】

热门文章

  1. Java虚拟机学习集锦是我攒来的,带你碾压面试官!
  2. python全栈开发中级班全程笔记(第二模块、第四章)(常用模块导入)
  3. CF 526F Max Mex(倍增求LCA+线段树路径合并)
  4. Anti-Aliasing SSAA MSAA MLAA SRAA 简介
  5. day28 socketserver
  6. JavaScript覆盖率统计实现
  7. 2017年读书计划(一)
  8. Bootstrap中水平排列的表单form-inline
  9. MyBatis入门(二)---一对一,一对多
  10. C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母