本文介绍了如何用undefined或null检查falsy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
undefined和null在javascript中是虚假的,但是
undefined and null are falsy in javascript but,
var n = null;
if(n===false){
console.log('null');
} else{
console.log('has value');
}
但是在控制台中尝试返回有值",为什么不为"null"?
but it returns 'has value' when tried in console, why not 'null' ?
推荐答案
要解决您的问题,请执行以下操作:
您可以使用not运算符(!):
To solve your problem:
You can use not operator(!):
var n = null;
if(!n){ //if n is undefined, null or false
console.log('null');
} else{
console.log('has value');
}
// logs null
回答您的问题:
对于布尔值,它被认为是虚假的或真实的.因此,如果您使用这种方式:
To answer your question:
It is considered falsy or truthy for Boolean. So if you use like this:
var n = Boolean(null);
if(n===false){
console.log('null');
} else{
console.log('has value');
}
//you'll be logged null
这篇关于如何用undefined或null检查falsy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!