在JavaScript中,如何判断值的类型?

JavaScript has a few built-in types, including numbers, strings, booleans, objects.

JavaScript具有一些内置类型,包括数字,字符串,布尔值,对象。

Using the typeof operator we can check what is the type of a value assigned to a variable.

使用typeof运算符,我们可以检查分配给变量的值的类型是什么。

For example:

例如:

typeof 'test'

Note that it’s not a function, it’s an operator, so parentheses are not required.

请注意,它不是函数,而是运算符,因此不需要括号。

Using it, we will get a string back, returning one of the following values:

使用它,我们将返回一个字符串,返回以下值之一:

  • 'number'

    'number'

  • 'string'

    'string'

  • 'boolean'

    'boolean'

  • 'undefined'

    'undefined'

  • 'bigint'

    'bigint'

  • 'symbol'

    'symbol'

  • 'object'

    'object'

  • 'function'

    'function'

Note that there is no null type, and

请注意,没有null类型,并且

typeof null

will return 'object'.

将返回'object'

Arrays will return 'object' too:

数组也将返回'object'

typeof [ 1 , 2 , 3 ] //'object'

Functions are a special kind of objects, as we can add properties and methods to functions:

函数是一种特殊的对象,因为我们可以向函数添加属性和方法:

const talk = () => {}
talk . test = true

but they have their own value 'function' if we use the typeof operator.

但是如果我们使用typeof运算符,它们将具有自己的值'function'

翻译自: https://flaviocopes.com/javascript-value-type/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值