- null 作为js原始类型之一,表示对象值为空。
- 使用typeof类型判断结果为Object
- 与
' '
和" "
不等价,这两个指的是字符串为空
- undefined 只定义未赋值会得到undefined
- 使用typeof类型判断会得到字符串类型的
'undefined'
- 使用typeof类型判断会得到字符串类型的
- undeclared 没有定义之前直接使用变量或函数会得到
undeclared
报错
至于怎么区分这三种类型,直接给出代码
const typeTest = val => {
typeof(val) === 'object' && console.log('null');
typeof(val) === 'undefined' && console.log('undefined');
}
try {
typeTest(null);
typeTest(undefined);
typeTest(a);
} catch(e) {
console.log(`undeclared : ${e}`)
}