判断js中的数据类型的几种方法

Posted gushixianqiancheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了判断js中的数据类型的几种方法相关的知识,希望对你有一定的参考价值。

来源:https://www.cnblogs.com/dushao/p/5999563.html
1、typeof,返回值是字符串并且首字母小写,可以判断function数据类型,在判断非Object类型的数据时比较方便。
```javascript:run
var myString = "he is string";
var myNumber = 222;
var myArray= [1,2,3];
var myDate = new Date();
var myObject = {a:1};
var myFunction = function(){console.log(111);};
console.log(typeof myString) // string
console.log(typeof myNumber) // number
console.log(typeof myArray) // object
console.log(typeof myDate) // object
console.log(typeof myObject) // object
console.log(typeof myFunction) // function
console.log(typeof myNumber == "number") // true
```
2、instanceof,返回值是布尔并且首字母小写
```javascript:run
var myString = "he is string";
var myNumber = 222;
var myArray= [1,2,3];
var myDate = new Date();
var myObject = {a:1};
var myFunction = function(){console.log(111);};
console.log(myString instanceof String) // true
console.log(myNumber instanceof Number) // true
console.log(myArray instanceof Array) // true
console.log(myDate instanceof Date) // true
console.log(myObject instanceof Object) // true
console.log(myFunction instanceof Function) // true
```
3、constructor,返回值是对象并且首字母大写
```javascript:run
var myString = "he is string";
var myNumber = 222;
var myArray= [1,2,3];
var myDate = new Date();
var myObject = {a:1};
var myFunction = function(){console.log(111);};
console.log(myString.constructor === String) // true
console.log(myNumber.constructor === Number) // true
console.log(myArray.constructor === Array) // true
console.log(myDate.constructor === Date) // true
console.log(myObject.constructor === Object) // true
console.log(myFunction.constructor === Function) // true
```
4、prototype,返回值是(数组)字符串。
```javascript:run
var myString = "he is string";
var myNumber = 222;
var myArray= [1,2,3];
var myDate = new Date();
var myObject = {a:1};
var myFunction = function(){console.log(111);};
console.log(Object.prototype.toString.call(myString) === "[object String]") // true;
console.log(Object.prototype.toString.call(myNumber) === "[object Number]") // true;
console.log(Object.prototype.toString.call(myArray) === "[object Array]") // true;
console.log(Object.prototype.toString.call(myDate) === "[object Date]") // true;
console.log(Object.prototype.toString.call(myObject) === "[object Object]") // true;
console.log(Object.prototype.toString.call(myFunction) === "[object Function]") // true;
```
5、jquery.type()
```javascript
var myUndefined=undefined;
var myNull=null;
var myString = "he is string";
var myNumber = 222;
var myArray= [1,2,3];
var myDate = new Date();
var myObject = {a:1};
var myFunction = function(){console.log(111);};
var myError = new Error();
var myRegexp = /test/;
console.log(jQuery.type(myUndefined) === "undefined");
console.log(jQuery.type(myNull) === "null");
console.log(jQuery.type(myString) === "string");
console.log(jQuery.type(myNumber) === "number");
console.log(jQuery.type(myArray ) === "array");
console.log(jQuery.type(myDate) === "date");
console.log(jQuery.type(myFunction) === "function");
console.log(jQuery.type(myError) === "error");
console.log(jQuery.type(myRegexp) === "regexp");
```

以上是关于判断js中的数据类型的几种方法的主要内容,如果未能解决你的问题,请参考以下文章

转:判断js中的数据类型的几种方法

判断js中的数据类型的几种方法

JS数据类型判断的几种方法

判断JS数据类型的几种方法

JS类型判断的几种方式

js中判断数据类型的几种实用方法