JS检测数据类型详解

发布时间:2023-05-23

一、js检测数据类型方法

JS中内置了用于检测数据类型的方法,其中最常用的是typeofinstanceof

// 使用typeof检测数据类型
console.log(typeof 123); // 输出 "number"
console.log(typeof "hello"); // 输出 "string"
console.log(typeof true); // 输出 "boolean"
console.log(typeof undefined); // 输出 "undefined"
console.log(typeof null); // 输出 "object"
console.log(typeof []); // 输出 "object"
console.log(typeof {}); // 输出 "object"
console.log(typeof function(){}); // 输出 "function"
// 使用instanceof检测数据类型
console.log(123 instanceof Number); // 输出 false
console.log("hello" instanceof String); // 输出 false
console.log(true instanceof Boolean); // 输出 false
console.log(undefined instanceof undefined); // 输出报错
console.log(null instanceof null); // 输出 false
console.log([] instanceof Array); // 输出 true
console.log({} instanceof Object); // 输出 true
console.log((function(){}) instanceof Function); // 输出 true

二、js如何检测数据类型

在JS中,除了使用内置的检测数据类型方法外,还可以使用typeof+值的形式来进行数据类型的判断。

// 使用typeof+值来检测数据类型
console.log(typeof 123 === "number"); // 输出 true
console.log(typeof "hello" === "string"); // 输出 true
console.log(typeof true === "boolean"); // 输出 true
console.log(typeof undefined === "undefined"); // 输出 true
console.log(typeof null === "object"); // 输出 true
console.log(typeof [] === "object"); // 输出 true
console.log(typeof {} === "object"); // 输出 true
console.log(typeof function(){} === "function"); // 输出 true

三、检测数据类型js

在JS中,有多种方法可以检测数据类型,如使用typeofinstanceofObject.prototype.toString等。

// 使用Object.prototype.toString来检测数据类型
console.log(Object.prototype.toString.call(123) === "[object Number]"); // 输出 true
console.log(Object.prototype.toString.call("hello") === "[object String]"); // 输出 true
console.log(Object.prototype.toString.call(true) === "[object Boolean]"); // 输出 true
console.log(Object.prototype.toString.call(undefined) === "[object Undefined]"); // 输出 true
console.log(Object.prototype.toString.call(null) === "[object Null]"); // 输出 true
console.log(Object.prototype.toString.call([]) === "[object Array]"); // 输出 true
console.log(Object.prototype.toString.call({}) === "[object Object]"); // 输出 true
console.log(Object.prototype.toString.call(function(){}) === "[object Function]"); // 输出 true

四、js检测数据类型是不是数字

在JS中,可以使用typeofisNaN方法来检测一个值是否为数字类型。

var num = 123;
console.log(typeof num === "number"); // 输出 true
console.log(!isNaN(num)); // 输出 true
var str = "123";
console.log(typeof str === "number"); // 输出 false
console.log(!isNaN(str)); // 输出 true
var bool = true;
console.log(typeof bool === "number"); // 输出 false
console.log(!isNaN(bool)); // 输出 true
var undef = undefined;
console.log(typeof undef === "number"); // 输出 false
console.log(isNaN(undef)); // 输出 true
var nul = null;
console.log(typeof nul === "number"); // 输出 false
console.log(!isNaN(nul)); // 输出 true

五、js检测数据类型的操作符

在JS中,有三种操作符可以用于检测变量的数据类型,分别为typeofinstanceof、和Object.prototype.toString.call()

var num = 123;
console.log(typeof num); // 输出 "number"
console.log(num instanceof Number); // 输出 false
console.log(Object.prototype.toString.call(num)); // 输出 "[object Number]"
var str = "hello";
console.log(typeof str); // 输出 "string"
console.log(str instanceof String); // 输出 false
console.log(Object.prototype.toString.call(str)); // 输出 "[object String]"
var bool = true;
console.log(typeof bool); // 输出 "boolean"
console.log(bool instanceof Boolean); // 输出 false
console.log(Object.prototype.toString.call(bool)); // 输出 "[object Boolean]"
var arr = [];
console.log(typeof arr); // 输出 "object"
console.log(arr instanceof Array); // 输出 true
console.log(Object.prototype.toString.call(arr)); // 输出 "[object Array]"
var obj = {};
console.log(typeof obj); // 输出 "object"
console.log(obj instanceof Object); // 输出 true
console.log(Object.prototype.toString.call(obj)); // 输出 "[object Object]"
var fn = function(){};
console.log(typeof fn); // 输出 "function"
console.log(fn instanceof Function); // 输出 true
console.log(Object.prototype.toString.call(fn)); // 输出 "[object Function]"

六、js检测数据类型的方法有哪些

JS中检测数据类型的方法有很多种,如上文所述,最常用的是typeofinstanceof、和Object.prototype.toString.call()。 除此之外,还有以下方法:

  1. 使用constructor方法进行数据类型的判断
var num = new Number(123);
console.log(num.constructor === Number); // 输出 true
var str = new String("hello");
console.log(str.constructor === String); // 输出 true
var bool = new Boolean(true);
console.log(bool.constructor === Boolean); // 输出 true
var arr = [];
console.log(arr.constructor === Array); // 输出 true
var obj = {};
console.log(obj.constructor === Object); // 输出 true
var fn = function(){};
console.log(fn.constructor === Function); // 输出 true
  1. 使用isPrototypeOf方法进行数据类型的判断
var num = new Number(123);
console.log(Number.prototype.isPrototypeOf(num)); // 输出 true
var str = new String("hello");
console.log(String.prototype.isPrototypeOf(str)); // 输出 true
var bool = new Boolean(true);
console.log(Boolean.prototype.isPrototypeOf(bool)); // 输出 true
var arr = [];
console.log(Array.prototype.isPrototypeOf(arr)); // 输出 true
var obj = {};
console.log(Object.prototype.isPrototypeOf(obj)); // 输出 true
var fn = function(){};
console.log(Function.prototype.isPrototypeOf(fn)); // 输出 true
  1. 使用Array.isArray方法判断是否为数组类型
var arr = [];
console.log(Array.isArray(arr)); // 输出 true
var obj = {};
console.log(Array.isArray(obj)); // 输出 false

七、检测数据类型的四种方式

JS中检测数据类型的四种方式是:typeofinstanceofObject.prototype.toString.call()constructor

var num = 123;
console.log(typeof num === "number"); // 输出 true
console.log(num instanceof Number); // 输出 false
console.log(Object.prototype.toString.call(num)); // 输出 "[object Number]"
console.log(num.constructor === Number); // 输出 true
var str = "hello";
console.log(typeof str === "string"); // 输出 true
console.log(str instanceof String); // 输出 false
console.log(Object.prototype.toString.call(str)); // 输出 "[object String]"
console.log(str.constructor === String); // 输出 true
var bool = true;
console.log(typeof bool === "boolean"); // 输出 true
console.log(bool instanceof Boolean); // 输出 false
console.log(Object.prototype.toString.call(bool)); // 输出 "[object Boolean]"
console.log(bool.constructor === Boolean); // 输出 true
var arr = [];
console.log(typeof arr === "object"); // 输出 true
console.log(arr instanceof Array); // 输出 true
console.log(Object.prototype.toString.call(arr)); // 输出 "[object Array]"
console.log(arr.constructor === Array); // 输出 true
var obj = {};
console.log(typeof obj === "object"); // 输出 true
console.log(obj instanceof Object); // 输出 true
console.log(Object.prototype.toString.call(obj)); // 输出 "[object Object]"
console.log(obj.constructor === Object); // 输出 true
var fn = function(){};
console.log(typeof fn === "function"); // 输出 true
console.log(fn instanceof Function); // 输出 true
console.log(Object.prototype.toString.call(fn)); // 输出 "[object Function]"
console.log(fn.constructor === Function); // 输出 true

八、怎样检测数据类型

在JS中,我们可以使用多种方式来检测一个值的数据类型,如 typeofinstanceofObject.prototype.toString.call()等。 对于基本数据类型,可以使用typeof或者isNaN方法来检测其数据类型;对于引用类型如数组和对象,可以使用instanceofObject.prototype.toString.call()或者constructor方法来进行判断。