当前位置: 首页 > Web前端 > HTML

js中undefined,null,0,false,[],{},''的区别

时间:2023-04-02 21:38:55 HTML

typeoftypeof0//numbertypeof''//stringtypeoffalse//booleantypeofundefined//undefinedtypeof[]//objecttypeofnull//objecttypeof{}//objecttypeof一共返回5种类型objectstringnumberundefinedboolean函数很容易混淆,因为JS有5种基本数据类型:Number、String、Boolean、Undefined、Null,还有一个复杂的基本类型object。Boolean()imagedescriptionBoolean(0)//falseBoolean(undefined)//falseBoolean(null)//falseBoolean('')//falseBoolean(false)//falseBoolean()//falseBoolean([])//trueBoolean({})//true==,===''==false//true[]==false//true0==false//truenull==undefined//truenull===undefined//falseundefined==false//falsenull==false//false{}==false//类型错误不考虑类型时,false,'',[],0都等于undefined可以用typeof来区分null可以用null===null来区分