用HTML写一个使用typeof运算符测试变量的类型 的代码
时间: 2024-02-25 08:51:54 浏览: 39
好的,以下是使用 typeof 运算符测试变量类型的 HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>测试变量类型</title>
</head>
<body>
<script>
var num = 42;
var str = "Hello, world!";
var bool = true;
var arr = ["apple", "banana", "orange"];
var obj = {name: "John", age: 30};
var undef;
var nul = null;
document.write("num 的类型为:" + typeof num + "<br>");
document.write("str 的类型为:" + typeof str + "<br>");
document.write("bool 的类型为:" + typeof bool + "<br>");
document.write("arr 的类型为:" + typeof arr + "<br>");
document.write("obj 的类型为:" + typeof obj + "<br>");
document.write("undef 的类型为:" + typeof undef + "<br>");
document.write("nul 的类型为:" + typeof nul + "<br>");
</script>
</body>
</html>
```
运行这段代码后,会在网页上显示每个变量的类型。请注意,typeof 运算符返回的是一个字符串,表示变量的类型。