JavaScript数据类型:Typeof解释

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

console.log(typeof null)  // object

更多信息: (More Information:)

MDN Documentation for typeof

有关typeof的MDN文档

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值