js数据类型1

1. 分类(2大类)

  • 基本(值)类型——5种
    • Number: 任意数值
    • String: 任意文本
    • Boolean: true/false
    • undefined: undefined
    • null: null
  • 对象(引用)类型——2种
    • Object: 任意对象
    • Array: 特别的对象类型(下标/内部数据有序)
    • Function: 特别的对象类型(可执行)

2. 判断

  • typeof:
    • 可以区别: 数值, 字符串, 布尔值, undefined, function
    • 不能区别: null与对象, 一般对象与数组
  • instanceof
    • 专门用来判断对象数据的类型: Object, Array与Function
  • ===
    • 可以判断: undefined和null
  1. 基本类型
 // typeof: 返回的是数据类型的 字符串 形式

 //1. 基本类型

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


  a = 3
  console.log(typeof a === 'number')  //true


  a = null
  console.log(typeof a) // 'object'
//2. 对象类型

var arr = [1,2,3];
typeof arr // "object"

var obj={name:'dada'};
typeof obj // "object"

var test = null;
typeof test // "object"

var fun= function(){};
typeof fun // "function"

typeof 检测一般对象、数组、null结果都是"object".
此时用 instanceof 

arr instanceof Array //true
obj instanceof Object //true
```

猜你喜欢

转载自www.cnblogs.com/maizilili/p/12365358.html