美文网首页
javascript 教程-数据类型 检查

javascript 教程-数据类型 检查

作者: sponing | 来源:发表于2016-12-07 08:55 被阅读0次
    数据类型

    字符串(String)、数字(Number)、布尔(Boolean)、数组(Array)、对象(Object)、空(Null)、未定义(Undefined)。

    检查数据类型的方法

    1,typeof
    2,instanceof

    typeof

    写法:typeof 变量/数据  
    typeof "John"                // 返回 string 
    typeof 3.14                  // 返回 number
    typeof false                 // 返回 boolean
    typeof [1,2,3,4]             // 返回 object
    typeof {name:'John', age:34} // 返回 object
    
    typeof undefined             // undefined
    typeof null                  // object 特殊的对象
    null === undefined           // false ===是数据类型的比较
    null == undefined           //true ==单单比较 没有数据类型
    

    对于数组 会返回object 因为数组是特殊的对象 这个检查数组就要用instanceof

    instanceof

    instanceof只会返回return true/false 真/假
    写法:变量/数据  instanceof 数据类型
    var a = [1,2,3,4];
    var b =  a instanceof Array;
    var c = a instaceof String ;
    console.log(b); //true
    console.log(c); //false
    

    相关文章

      网友评论

          本文标题:javascript 教程-数据类型 检查

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