js高级_89、数据类型
1分类
基本(值)类型
String:任意字符串
Number:任意数字
Boolean:true/false
Null:null
Undefined:undefined
对象(引用)类型
Object:任意的对象(一般对象内部数据无序)
Function:一种特别的对象(可以执行)
Array:一种特别的对象(数值下标,内部数据是有序的)
2判断
typeof:返回数据类型的字符串表达
—-可以区别: 数值, 字符串, 布尔值, undefined, function
—-不能区别: null与对象, 一般对象与数组
instanceof
专门用来判断对象数据的类型: Object, Array与Function
===
可以判断: undefined和null
①. 基本类型
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 = ‘atguigu’
console.log(typeof a === ‘string’)// true
a = true
console.log(typeof a === ‘boolean’)// true
a = null
console.log(a===null) // true
console.log(typeof a) // ‘object’
②. 对象类型
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.b22) // ‘abc’ undefined