JavaScript 检查值是否仅为未定义、空或假
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6836865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript check if value is only undefined, null or false
提问by Lime
Other than creating a function, is there a shorter way to check if a value is undefined
,null
or false
only in JavaScript?
除了创建函数之外,是否有更短的方法来检查值是否为undefined
,null
或者false
仅在 JavaScript 中?
The below if statement is equivalent to if(val===null && val===undefined val===false)
The code works fine, I'm looking for a shorter equivalent.
下面的 if 语句等效于if(val===null && val===undefined val===false)
代码工作正常,我正在寻找更短的等效项。
if(val==null || val===false){
;
}
Above val==null
evaluates to true both when val=undefined
or val=null
.
以上val==null
计算为 true 时val=undefined
或val=null
。
I was thinking maybe using bitwise operators, or some other trickery.
我在想也许使用按位运算符或其他一些技巧。
采纳答案by hugomg
Well, you can always "give up" :)
好吧,你总是可以“放弃”:)
function b(val){
return (val==null || val===false);
}
回答by David Hoffman
The best way to do it I think is:
我认为最好的方法是:
if(val != true){
//do something
}
This will be true if val is false, NaN, or undefined.
如果 val 为 false、NaN 或未定义,则为 true。
回答by SudoPlz
I think what you're looking for is !!val==false
which can be turned to !val
(even shorter):
我认为你正在寻找的是!!val==false
可以转向!val
(甚至更短):
You see:
你看:
function checkValue(value) {
console.log(!!value);
}
checkValue(); // false
checkValue(null); // false
checkValue(undefined); // false
checkValue(false); // false
checkValue(""); // false
checkValue(true); // true
checkValue({}); // true
checkValue("any string"); // true
That works by flipping the value by using the !
operator.
这是通过使用!
运算符翻转值来工作的。
If you flip null
once for example like so :
如果你null
像这样翻转一次:
console.log(!null) // that would output --> true
If you flip it twice like so :
如果你像这样翻转两次:
console.log(!!null) // that would output --> false
Same with undefined
or false
.
与undefined
或相同false
。
Your code:
您的代码:
if(val==null || val===false){
;
}
would then become:
然后会变成:
if(!val) {
;
}
That would work for all cases even when there's a string but it's length is zero.
Now if you want it to also work for the number 0 (which would become false
if it was double flipped) then your if would become:
即使有字符串但长度为零,这也适用于所有情况。现在,如果您希望它也适用于数字 0(false
如果它被双翻转就会变成),那么您的 if 将变成:
if(!val && val !== 0) {
// code runs only when val == null, undefined, false, or empty string ""
}
回答by Yiding
Another solution:
另一种解决方案:
Based on the document, Boolean object will return true if the value is not 0, undefined, null, etc. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
根据文档,如果值不是 0、undefined、null 等,Boolean 对象将返回 true。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
If value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false.
如果 value 被省略或为 0、-0、null、false、NaN、undefined 或空字符串 (""),则对象的初始值为 false。
So
所以
if(Boolean(val)) { //executable... }
回答by Eugene Lazutkin
One way to do it is like that:
一种方法是这样的:
var acceptable = {"undefined": 1, "boolean": 1, "object": 1};
if(!val && acceptable[typeof val]){
// ...
}
I think it minimizes the number of operations given your restrictions making the check fast.
我认为鉴于您的限制使检查速度更快,它可以最大限度地减少操作次数。
回答by Joseph Marikle
only shortcut for something like this that I know of is
我所知道的这种事情的唯一捷径是
var val;
(val==null || val===false) ? false: true;
回答by Brigette
Boolean(val) === false. This worked for me to check if value was falsely.
布尔值 (val) === 假。这对我有用以检查值是否错误。
回答by Pedro Pereira
Using ? is much cleaner.
使用 ?干净多了。
var ? function_if_exists() : function_if_doesnt_exist();
回答by Ratan Uday Kumar
Try like Below
尝试如下
var Boolify = require('node-boolify').Boolify;
if (!Boolify(val)) {
//your instruction
}
Refer node-boolify