JS中的变量类型

变量

  1. JS 中使用 typeof 能得到哪些类型

    typeof undefined //undefined
    typeof 'abc' //string
    typeof 123 //number
    typeof true //boolean
    //以上四点可以看出,typeof只能区分值类型的详细类型
    typeof {} //object
    typeof [] //object
    typeof null //object
    typeof console.log //function
    
  2. 何时使用 === 和 ==

    if (obj.a == null) {
      //这里相当于obj.a === null || obj.a === undefined的简写形式
      // 这是jqurey源码中推荐的写法
    }
    
  3. JS 中有哪些内置函数

    Object
    Array
    Boolean
    Number
    String
    Function
    Date
    RegExp
    Error
    
  4. JS 变量按照储存方式区分为哪些类型,并描述其特点

    • 变量类型

      1. 值类型
      var a = 100
      var b = a
      a = 200
      console.log(b) //100
      
      1. 引用类型
      var a = { age: 20 }
      var b = a
      b.age = 21
      console.log(a.age) //21
      
  5. 如何理解 JSON

    //JSON只不过是一个JS对象
    JSON.stringify({ a: 10, b: 20 })
    JSON.parse('{"a":10,"b":20}')
    

变量计算 - 强制类型转换

  1. 字符串拼接

    var a = 100 + 10 //110
    var b = 100 + '10' //'10010'
    
  2. ==运算符

    100 == '100' //true
    0 == '' //true
    null == undefined //true
    
  3. if 语句

    var a = true
    if (a) {
      //...
    }
    var b = 100
    if (b) {
      //...
    }
    var c = ''
    if (c) {
      //...
    }
    
  4. 逻辑运算符

    console.log(10 && 0) //0
    console.log('' || 'abc') //'abc'
    console.log(!window.abc) //true
    //判断一个变量会被当做true还是false
    var a = 100
    console.log(!!a) //true
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值