JavaScript 如何获取值的原生类型
JavaScript变量可以存储任何类型的值。可以使用JavaScript typeof运算符来确定值的原生类型。
它返回一个指示值类型的字符串。
语法:
typeof(value)
typeof 可能返回的输出值:
| Type | Result |
|---|---|
| undefined | “undefined” |
| null | “object” |
| boolean | “boolean” |
| number | “number” |
| string | “string” |
| symbol | “symbol” |
| function | “function” |
| object | “object” |
示例:
<!DOCTYPE html>
<html>
<body>
<h2>Function returns native type of the value</h2>
<strong>Output: </strong><br />
<div id="output"></div>
<script>
document.getElementById("output").innerHTML =
typeof 98 + " " + typeof "geeksforgeeks" + " " + typeof null;
</script>
</body>
</html>
输出:

极客教程