JavaScript教程-JavaScript 的 typeof 运算符
JavaScript 的 typeof 运算符用于返回表示给定值的 JavaScript 类型的字符串。它以字符串形式返回操作数的数据类型。操作数可以是文字值或数据结构,如函数、对象或变量。
语法
使用 typeof 运算符有以下两种方式。
typeof 操作数
或者
typeof (操作数)
值
操作数: 它是一个表达式,表示要返回其类型的对象或基本类型。
typeof 运算符的可能返回值如下表所示:
操作数的类型 | 结果 |
---|---|
object | "object" |
number | "number" |
string | "string" |
boolean | "boolean" |
function | "function" |
undefined | "undefined" |
让我们通过一些示例来理解这个运算符。
示例1
在这个示例中,操作数是数字类型。typeof 运算符将打印操作数的类型,即 "number",无论操作数是负整数、浮点数、无穷大、NaN、零还是任何整数。
<!DOCTYPE html>
<html>
<head>
<title>typeof 运算符示例</title>
</head>
<body>
<script>
document.write(typeof (45) + "**<br>**"); // 结果:"number"
document.write(typeof (-90) + "**<br>**"); // 结果:"number"
document.write(typeof (0) + "**<br>**"); // 结果:"number"
document.write(typeof (22.6) + "**<br>**"); // 结果:"number"
document.write(typeof (Infinity) + "**<br>**"); // 结果:"number"
document.write(typeof (NaN)); // 结果:"number"。虽然 NaN 是 "Not-A-Number"
</script>
</body>
</html>
输出
执行上述代码后,输出将为:
number
number
number
number
number
number
示例2
在这个示例中,操作数是字符串类型。typeof 运算符将打印操作数的类型,即 "string",无论操作数是空字符串、字符集合还是用引号括起来的数字。
<!DOCTYPE html>
<html>
<head>
<title>typeof 运算符示例</title>
</head>
<body>
<script>
document.write(typeof ("") + "**<br>**"); // 结果:"string"
document.write(typeof ("javaTpoint.com") + "**<br>**"); // 结果:"string"
document.write(typeof ("20") + "**<br>**"); // 结果:"string"
document.write(typeof (typeof 1) + "**<br>**"); // 结果:"string"
document.write(typeof String(12)); // 使用 String 函数封装将结果包装为:"string"
</script>
</body>
</html>
输出
执行上述代码后,输出将为:
string
string
string
string
string
示例3
在这个示例中,操作数是布尔类型。typeof 运算符将打印操作数的类型,即 "boolean",无论操作数是 true 还是 false。
<!DOCTYPE html>
<html>
<head>
<title>typeof 运算符示例</title>
</head>
<body>
<script>
document.write(typeof (true) + "**<br>**"); // 结果:"boolean"
document.write(typeof (false) + "**<br>**"); // 结果:"boolean"
document.write(typeof Boolean(20) + "**<br>**"); // 使用 Boolean 函数封装将结果包装为:"boolean"
</script>
</body>
</html>
输出
执行上述代码后,输出将为:
boolean
boolean
boolean
示例4
在这个示例中,操作数是未定义类型。typeof 运算符将打印操作数的类型,即 "undefined"。在这里,Null 的类型是 undefined,这是因为它被写成了 Null,而不是 null。如果写成 null,它的类型将是 object。
<!DOCTYPE html>
<html>
<head>
<title>typeof 运算符示例</title>
</head>
<body>
<script>
document.write(typeof Null + "**<br>**"); // 结果:"undefined"
document.write(typeof undefined + "**<br>**"); // 结果:"undefined"
document.write(typeof a + "**<br>**"); // 结果:"undefined"
</script>
</body>
</html>
输出
执行上述代码后,输出将为:
undefined
undefined
undefined
示例5
在这个示例中,操作数是 Object 和 Function 类型。typeof 运算符将根据操作数的类型打印 "object" 和 "function"。
<!DOCTYPE html>
<html>
<head>
<title>typeof 运算符示例</title>
</head>
<body>
<script>
document.write(typeof null + "**<br>**"); // 结果:"object"
document.write(typeof [1, 2, 'hello'] + "**<br>**"); // 结果:"object"
document.write(typeof {a: 'hello'} + "**<br>**"); // 结果:"object"
document.write(typeof [1, 2, 3, 4] + "**<br>**"); // 结果:"object"
document.write(typeof function(){} + "**<br>**"); // 结果:"function"
document.write(typeof class hello{} + "**<br>**"); // 结果:"function"
</script>
</body>
</html>
输出
执行上述代码后,输出将为:
object
object
object
object
function
function