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


本文摘自PHP中文网,作者WJ,侵删。

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

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

instanceof:判断具体的对象类型

===:用于判断undefined和null

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

//五种基本类型

    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和===的区别的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

分享5种js字符串转数字的方法

js 代码要不要加分号?

利用js模仿360开机效果

javascript如何解除绑定事件

vue.js监听键盘事件

如何在html中使用javascript

爬虫分析之 js逆向某验滑动加密(1)

js 怎么判断数字相等

怎么获取json的key

三种js数组去重的简洁方案

更多相关阅读请进入《js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...