温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

instanceof与typeof以及===有什么区别

发布时间:2020-06-01 21:00:31 来源:亿速云 阅读:317 作者:鸽子 栏目:web开发

js中的typeof和instanceof和===的区别

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和===的区别的详细内容,更多请关注亿速云其它相关文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI