js基本数据类型

基本数据类型有:number、String、Boolean、null、undefined、Object

1.

let a = 2
console.log(typeof a)
//number

2.

let a = 'aaa'
console.log(typeof a)
//string

3.

let a = true
console.log(typeof a)
//boolean

4.

let a = null
console.log(typeof a)
//object

5.

let a = undefined
console.log(typeof a)
//undefined

6.

let a = {}
console.log(typeof a)
//object

7.

let a = [1,2]
console.log(typeof a)
//object

8.

let a = function(){}
console.log(typeof a)
//function

小结

 * typeof:

   * 可以区别: 数值, 字符串, 布尔值, undefined, function

   * 不能区别: null与对象, 一般对象与数组

 * instanceof

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

 * ===

   * 可以判断: undefined和null

判断

1.

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

2.

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'

console.log(typeof b1.b2[2]) // 'function'
console.log(b1.b2[2]('abc')) // 'abc' undefined

其他

1. undefined与null的区别?

 * undefined代表没有赋值

 * null代表赋值了, 只是值为null

 

2. 什么时候给变量赋值为null呢?

 * var a = null //a将指向一个对象, 但对象此时还没有确定

 * a = null //让a指向的对象成为垃圾对象

 

3. 严格区别变量类型与数据类型?

 * js的变量本身是没有类型的, 变量的类型实际上是变量内存中数据的类型

 * 变量类型:

   * 基本类型: 保存基本类型数据的变量

   * 引用类型: 保存对象地址值的变量

 * 数据对象

   * 基本类型

   * 对象类型

1.

// 1. undefined与null的区别?
var a1
var a2 = null
console.log(a1, a2)
//undefined null

2.

// 2. 什么时候给变量赋值为null呢?
    //初始
  var a3 = null
    //中间
  var name = 'Tom'
  var age = 12
  a3 = {
    name: name,
    age: age
  }
    //结束
  a3 = null

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值