1、原始值类型
/**
* 未定义 —— Undefined
* 字符串 —— String
* 数 字 —— Number
* 布 尔 —— Boolean
* 空 值 —— Null
* Symbol —— ES6 引入了一种新的原始数据类型,表示独一无二的值
**/
// JavaScript 拥有动态类型
var a; // undefined
var a = 'eben' // 字符串
var a = 0 // number
// =================================================
// 原始值类型可以使用typeof进行判断
var a;
if(typeof a === 'undefined') console.log('undefined')
var a = 'eben'
if(typeof a === 'string') console.log('string')
var a = 0
if(typeof a === 'number') console.log('number')
var a = true
if(typeof a === 'boolean') console.log('boolean')
// null 直接判断 ===
var a = null
if (a === null) console.log('null')
// 唯一值
var a = Symbol('onlyOne') // 唯一
if(typeof a === 'symbol') console.log('symbol')
var b = Symbol('onlyOne') // 唯一
console.log(a===b) // false
/**----**/
2、引用数据类型
/**
* 对象 —— Object
* 数组 —— Array
* 函数 —— Function
**/
// instanceof 判断数据类型
var obj = {}
if (obj instanceof Object) console.log('是对象')
var arr = []
if (arr instanceof Array) console.log('是数组')
var fn = () => { console.log('我是函数') };
if (fn instanceof Function) {
fn()
}
// 判断空对象
// 1、JSON.stringinfo()
if (JSON.stringify(obj) === '{}') console.log('1-空对象')
// 2、遍历属性
var isObj = false
for (var item in obj) {
if (item) isObj = true
}
if (!isObj) console.log('2-空对象')
/**----**/