undefined 和 null 在 javascript 中是假的,但是,
var n = null;
if(n===false){
console.log('null');
} else{
console.log('has value');
}
但在控制台中尝试时它返回“有值”,为什么不返回“null”?
最佳答案
解决您的问题:
你可以使用 not 运算符(!):
var n = null;
if(!n){ //if n is undefined, null or false
console.log('null');
} else{
console.log('has value');
}
// logs null
回答你的问题:
bool 值被认为是假的或真值。所以如果你这样使用:
var n = Boolean(null);
if(n===false){
console.log('null');
} else{
console.log('has value');
}
//you'll be logged null
关于javascript - 如何检查 undefined 或 null 的 falsy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24730166/