首頁  >  文章  >  web前端  >  js中的typeof和instanceof和===的差別

js中的typeof和instanceof和===的差別

WJ
WJ轉載
2020-05-30 10:52:112359瀏覽

js中的typeof和instanceof和===的差別

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和===的全部。

相關參考:php中文網

#

以上是js中的typeof和instanceof和===的差別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51dev.com。如有侵權,請聯絡admin@php.cn刪除