JavaScript 中查看类型的方法:typeof 运算符返回原始类型 (string、number 等)。instanceof 运算符检查对象是否属于某个构造函数或其子类。Object.prototype.toString.call() 返回对象的类型格式为 "[object Type]”。Array.isArray() 检查变量是否为数组。Date.prototype.toString() 返回日期对象的字符串表示,可判断对象类型是否为 Date。

如何查看 JavaScript 类型
在 JavaScript 中,可以通过以下方法查看变量或表达式的类型:
1. typeof 运算符
typeof 运算符返回变量或表达式的原始类型(string、number、boolean、undefined、null 等)。
console.log(typeof "hello"); // "string" console.log(typeof 10); // "number" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined"
2. instanceof 运算符
instanceof 运算符用于检查对象是否属于某个构造函数或其子类。
console.log("hello" instanceof String); // false
console.log(new String("hello") instanceof String); // true3. Object.prototype.toString.call() 方法
中国地图网点分布情况提示查看特效JS代码,网点标注内容可以放图片、地址、电话信息,通常用在 公司网点全国分布点查询,例如快递网点、分公司网点,还是很实用的功能,基于jQuery实现。
Object.prototype.toString.call() 方法返回对象的类型,格式为 "[object Type]"(例如 "[object String]"、"[object Number]" 等)。
console.log(Object.prototype.toString.call("hello")); // "[object String]"
console.log(Object.prototype.toString.call(10)); // "[object Number]"4. Array.isArray() 方法
Array.isArray() 方法用于检查变量是否为数组。
console.log(Array.isArray("hello")); // false
console.log(Array.isArray([1, 2, 3])); // true5. Date.prototype.toString() 方法
Date.prototype.toString() 方法返回日期对象的字符串表示,可以用来判断对象的类型是否为 Date。
console.log(new Date().toString() instanceof Date); // true









