JS 判断数据类型的方法

一、JS 中的数据类型

● 基本数据类型:String 、Number 、undefined 、Null 、Boolean
● 引用数据类型:Object(Array、Function、Date…)
● ES6新增数据类型:Symbol、bigInt

二、JS 中判断数据类型的方法

1、使用 typeof

let obj = {
    name: 'john',
    age: 20,
    sex: '男'
}
let fn = function() {
    console.log('我是 function 类型');
}
console.log(typeof 1);       //number
console.log(typeof 'abc');   //string
console.log(typeof true);    //boolean
console.log(typeof undefined);  //undefined 
console.log(typeof fn);      //function
console.log(typeof (new Date) );  //object
console.log(typeof null);     //object
console.log(typeof [1,2,3]);  //object
console.log(typeof obj);      //object

【注意】:
● 对于基本类型,除 null 以外,均可以返回正确的结果。
● 对于引用类型,除 function 以外,一律返回 object 类型。
● 对于 null ,返回 object 类型。
● 对于 function 返回 function 类型。

2、使用 instanceof

instanceof一般用来检测引用数据类型,表达式为:A instanceof B,判断A是否是B的实例,如果 A 是 B 的实例,则返回 true,否则返回 false,由构造类型判断出数据类型。

let arr=[1,2,3,4]
let obj={
    name: 'john',
    age: 20,
    sex: '男'
}
let fn=function() {
    console.log('我是 function 类型');
}

console.log(arr instanceof Array);  //true
console.log(obj instanceof Object);  //true
console.log(fn instanceof Function);  //true
console.log((new Date) instanceof Date);  //true

3、 使用constructor

constructor是prototype对象上的属性,指向构造函数,注意:除了undefined和null之外,其他类型都可以通过constructor属性来判断类型。

let arr = [1, 2, 3, 4]
let obj = {
    name: 'john',
    age: 20,
    sex: '男'
}
let fn = function () {
   console.log('我是 function 类型');
}

console.log((9).constructor === Number);  //true
console.log('hello'.constructor === String);  //true
console.log(true.constructor === Boolean);  //true
console.log(fn.constructor === Function);  //true
console.log((new Date).constructor === Date);  //true
console.log(obj.constructor === Object);  //true
console.log([1, 2, 3].constructor === Array);  //true

4、使用Object.prototype.toString.call()

toString是Object原型对象上的一个方法,该方法默认返回其调用者的具体类型更严格的讲,是 toString运行时this指向的对象类型, 返回的类型格式为[object,xxx],xxx是具体的数据类型,其中包括:String,Number,Boolean,Undefined,Null,Function,Date,Array,RegExp,Error,HTMLDocument等等都可以通过这个方法获取到。

let obj = {
	name: 'john',
	age: 20,
	sex: '男'
}
let fn = function () {
	console.log('我是 function 类型');
}

console.log(Object.prototype.toString.call(1));        // [object Number]
console.log(Object.prototype.toString.call('Hello tomorrow')); // [object String ]
console.log(Object.prototype.toString.call(true));     // [object Boolean]
console.log(Object.prototype.toString.call(undefined));  // [object Undefined]
console.log(Object.prototype.toString.call(fn));   // [object Function]
console.log(Object.prototype.toString.call(new Date));  // [object Date]
console.log(Object.prototype.toString.call(null));   // [object Null]
console.log(Object.prototype.toString.call([1, 2, 3]));  // [object Array]
console.log(Object.prototype.toString.call(obj));       // [object Object]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端~初学者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值