javaScript判断数据类型以及typeof和intanceof的差别

本文介绍了JavaScript中的六种数据类型:Undefined、Null、Boolean、Number、String及Object,并详细阐述了如何使用typeof和instanceof来判断变量的具体类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、JS有6种数据类型:5种简单数据类型( Undefined,Null.Boolean,Number,String)、1种复杂数据类型 Object;


二、JS提供两种方式判断数据的类型: typeof和intanceof;

三、typeof返回结果包括:undefined,boolean,string,number,object,function

1、返回undefined,表示这个值为定义

<span style="color:#cc7832;font-weight:bold;">var </span>message<span style="color:#cc7832;">;

console.log(typeof message) //undefined 表示这个值未定义

var message = true;
console.log(typeof message) //boolean 表示这个值是布尔值

var message = ‘hello’;
console.log(typeof message) //string 表示这个值是字符串

var message = 123;
console.log(typeof message) //number 表示这个值是数值

var message = {};
console.log(typeof message) //object 表示这个值是对象

var message = null;
console.log(typeof message) //object 表示这个值为null

var message = function(){
console.log(‘hello’)
}
console.log(typeof message) //function 表示这个值为函数
四、instanceof与typeof区别在于instanceof判断一个实例是否属于某种类型,返回true或者false
function Person(name,age) {
this.name = name;
this.age = age;
}
var person1 = new Person(‘nike’,23);
console.log(person1 instanceof Object) ;//true;
console.log(person1 instanceof Person) ;//true;

转自:https://blog.youkuaiyun.com/yaomengzhi/article/details/78296534

  

   

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值