美文网首页
js检测数据类型

js检测数据类型

作者: 漠小涵 | 来源:发表于2020-10-21 16:15 被阅读0次

    1.基本数据类型:String、Number、Boolean、undefined、null
    2.引用数据类型:Object、Array、Date、RegExp、Function

    检测基本类型用typeof
    检测引用类型用instanceof

    var u;
    typeof "hello world";     //string
    typeof 22;                    //number
    typeof true;                  //boolean
    typeof u;                      //undefined
    typeof null;                  //object
    

    引用类型用typeof都是object

    var now = new Date();
    var x = {};
    var y = function(){};
    now instanceof Date;       //true
    [] instanceof Array;         //true
    x instanceof Object        //true 
    y instanceof Function     // true 
    

    相关文章

      网友评论

          本文标题:js检测数据类型

          本文链接:https://www.haomeiwen.com/subject/hmcjmktx.html