编写一个JavaScript函数checkDataType,该函数接受一个参数,并返回该参数的数据类型
时间: 2024-10-13 19:12:42 浏览: 39
在JavaScript中,你可以编写一个名为`checkDataType`的函数,使用`typeof`操作符来检查传入参数的数据类型。下面是一个简单的实现示例:
```javascript
function checkDataType(param) {
// 使用typeof运算符获取参数的类型
var dataType = typeof param;
// 返回数据类型的字符串描述
return dataType;
}
// 测试函数
console.log(checkDataType(10)); // 输出 "number"
console.log(checkDataType("Hello")); // 输出 "string"
console.log(checkDataType(true)); // 输出 "boolean"
console.log(checkDataType({})); // 输出 "object"
```
这个函数会返回一个表示参数类型名称的字符串,如"number", "string", "boolean", 或"object"等。如果你想得到更具体的类型信息,例如数组、null、undefined或函数,可以稍微修改一下函数,比如添加对特殊类型的检查。
阅读全文
相关推荐


















