javascript 数据类型 随笔

基本数据类型

var a =1 // "number"

var b ="" // "string"

var c =true // "boolean"

var d = null // "object"

var e // "undefined"

var s = Symbol() // "symbol"

// 注意
null == undefined // true
null == {} // false
undefined == {} //false

引用数据类型

var o = {"a":1} // "object"

数据类型判断

  1. typeof

    SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
     typeof null  //   "object"
    
     typeof [] //    "object"
    
     typeof {} //   "object"
  2. instanceof

    不能检测 null undefined

     // object
    ({}) instanceof Object //    true
    
     //null
     null instanceof Object //    false
    
     //array
     [] instanceof Array //     true
    
     //error
     null instanceof Null
     VM1344:1 Uncaught ReferenceError: Null is not defined
         at <anonymous>:1:17
     (anonymous) @ VM1344:1
     undefined instanceof Undefined
     VM1366:1 Uncaught ReferenceError: Undefined is not defined
         at <anonymous>:1:22
  3. Object.protorype.toSting.call() 最靠谱,最常用

    Object.prototype.toString.call("fd") //     "[object String]"
     Object.prototype.toString.call(1) //     "[object Number]"
     Object.prototype.toString.call(true) //     "[object Boolean]"
     Object.prototype.toString.call(null) //     "[object Null]"
     Object.prototype.toString.call(undefined) //     "[object Undefined]"
     Object.prototype.toString.call(Symbol()) //     "[object Symbol]"
     Object.prototype.toString.call({}) //     "[object Object]"
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄