JavaScript进阶篇(一)——数据类型

数据类型

一、分类
1、基本(值)数据类型
  • String:任意字符串
  • Number:任意数字
  • Boolean:true/false
  • Undefined: undefined
  • null:null
2、对象(引用)类型
  • Object:任意对象,Function和Array本质上也是对象,是特殊的对象
  • Function:一种特别的对象(可以执行)
  • Array:一种特别的对象(数值下标访问,内部数据是有序的)
二 、如何判断数据类型
  • typeof:返回数据类型的字符串表达;可以判断undefined、数值、字符串、布尔值、function;
    不能判断null与object、object与array
 	  let a
      console.log(a, typeof a)  // undefined "undefined"
      console.log(a === undefined, typeof a === 'undefined')  // true true
      a = 3
      console.log(typeof a === 'number')  // true
      a = 'hello'
      console.log(typeof a === 'string')  // true
      a = true
      console.log(typeof a === 'boolean') // true
      a = null
      console.log(typeof a, a === null)  // object true
  • instanceof:返回布尔值;判断对象具体类型
		let b = {
		        b1: [1, 2, console.log],
		        b2: function () {
		          console.log('hello')
		        }
		      }
      console.log(b instanceof Object)  // true
      console.log(b.b1 instanceof Array, b.b1 instanceof Object)   // true true
      console.log(b.b2 instanceof Function, b.b2 instanceof Object)  // true true
      console.log(typeof b.b2 === 'function')  // true
  • ===:可以判断undefined、null
undefined与null区别

undefined: 定义了但未赋值;
null: 定义了并且已经赋值,赋值为null

	  let c
      console.log(c)   // undefined
      c = null
      console.log(c)  // null
什么时候给变量赋值为null
  • 初始赋值为null,表明将要赋值为对象
  • 结束前,让对象成为垃圾对象(被垃圾回收器回收)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值