[英]Is there any difference in using !! vs using checks for null | undefined? in javascript/typescript
I was wondering if there is any difference when using !
我想知道使用时是否有任何区别!
vs checks like isNullOrUndefined
or even custom checks like vs 像isNullOrUndefined
这样的检查,甚至像这样的自定义检查
customIsNullOrUndefined(obj: any) { return obj === null || obj === undefined;}
vs对比
!obj
vs对比
util.isNullOrUndefined(obj)
I know that util.isNullOrUndefined
is deprecated我知道util.isNullOrUndefined
已被弃用
I mean semantically !obj should return the same as the checks for null |我的意思是语义上 !obj 应该返回与 null 的检查相同的结果 | undefined不明确的
!obj
will evaluate to true
for any falsy value . !obj
对于任何虚假值都会评估为true
。 That means null
or undefined
, but also things like 0
or ""
.这意味着null
或undefined
,还有像0
或""
这样的东西。 You can use this if you're sure you're only checking objects and not primitive values.如果您确定只检查对象而不是原始值,则可以使用它。
The not operator of !
的 not 运算符!
determines whether a value is falsy, which includes the following:确定一个值是否为假,包括以下内容:
false
0
undefined
null
''
If a function checks for null
or undefined
, then it will not check for other falsy values, such as false
, for example.例如,如果函数检查null
或undefined
,则它不会检查其他虚假值,例如false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.