美文网首页
入坑前端07:JS 里的数据类型

入坑前端07:JS 里的数据类型

作者: 万事屋小酱 | 来源:发表于2019-03-15 11:35 被阅读0次

    ●基本数据类型

    String:任意字符串
    Number:任意的数字
    boolean:true/false
    undefined:undefined
    null:null

    ●引用(对象)数据类型

    Object:任意对象
    Function:一种特别的对象(可以执行)
    Array:一种特别的对象(数值下标,内部数据是有序的)

    判断数据类型

    1.typeof

    注意:typeof返回的是数据类型的字符串!!表达

    var a
      console.log(a, typeof a, a===undefined) // undefined 'undefined' true
      console.log(a===typeof a) // false
    
    a = 'atguigu'
    console.log(typeof a === 'string') 
    

    但是typeof不能判断null和对象

    a=null
    console.log(a===null)
    console.log(typeof a)
    

    所以typeof可以区别:数值, 字符串, 布尔值, undefined, function但不能区别null与对象

    2.instanceof

    专门用来判断对象的数据类型:
    Object ,Array,Function

    var b1 = {
        b2: [2, 'abc', console.log],
        b3: function () {
          console.log('b3()')
        }
      }
    console.log(b1 instanceof Object, typeof b1) // true 'object' 
    console.log(b1.b2 instanceof Array, typeof b1.b2) // true 'object'
    console.log(b1.b3 instanceof Function, typeof b1.b3) // true 'function'
    

    3,全等(===)

    ===可以判断:undefined和null

    a = null
      console.log(a===null) // true
    
    var a
    console.log(a===undefined) //true
    

    相关文章

      网友评论

          本文标题:入坑前端07:JS 里的数据类型

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