html5里inst,浅谈js中的typeof、instanceof和===

本文详细探讨了JavaScript中用于类型检查的typeof、instanceof和===操作符的用法。typeof能判断基本类型,但无法区分null和object;instanceof则用于判断对象所属的具体类型;===则用于严格相等比较,包括undefined和null。通过实例展示了它们的使用情况,对于理解和运用这些操作符有很好的指导意义。
摘要由CSDN通过智能技术生成

本篇文章就来浅谈一下js中的typeof、instanceof和===。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

5ed77897bcf51568.jpg

typeof:用于判断number/string/boolean/underfined类型/function,不能判断:null和object ,不能区分object和Array

instanceof:判断具体的对象类型

===:用于判断undefined和null//五种基本类型

var num=1; var str="abc"; var bl=true; var nu=null; var undef=undefined; //三种特殊类型

var obj=new Object(); var arr2=["1",2,true]; var fun=function () {

}

write("-------typeof-----------")

write(num,typeof num);//1 number

write(str,typeof str);//abc string

write(bl,typeof bl);//true boolean

write(nu,typeof nu);//null object

write(undef,typeof undef)//undefined undefined

write(obj,typeof obj);//[object Object] object

write(arr2,typeof arr2);//1,2,true object

write("-----------===-----------")

write(num,typeof num==="number");//1 true

write(str,typeof str==="string");//abc true

write(bl,typeof bl==="boolean");//true true

write(nu,typeof nu==="object");//null true

write(undef,typeof undef==="undefined")//undefined true

write(obj,typeof obj==="object");//[object Object] true

write(arr2,typeof arr2==="object");//1,2,true true

write(fun,typeof fun==="function");//function () { } true

write("---------instanceof---------------")

write(obj,obj instanceof Object)//[object Object] true

write(arr2,arr2 instanceof Array);//1,2,true true

write(arr2,arr2 instanceof Object);//1,2,true true

write(fun, fun instanceof Function)//function () { } true

write(fun, fun instanceof Object)//function () { } true

以上就是js中的typeof和instanceof和===的全部内容。

更多web前端知识,请查阅 HTML中文网 !!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值