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'
。