简体   繁体   English

使用上有什么区别吗!! vs 使用 null 检查 | 不明确的? 在 javascript/打字稿中

[英]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 "" .这意味着nullundefined ,还有像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.例如,如果函数检查nullundefined ,则它不会检查其他虚假值,例如false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 javascript 中使用 != null 和 != undefined 之间有什么功能区别吗? - Is there any function difference between using != null and != undefined in javascript? 正在使用&&和||快捷键typeof ==“undefined”在javascript中检查稳定? - Is using && and || to shortcut typeof == “undefined” checks stable in javascript? 使用 Javascript 递归检查 object 中的任何 null/未定义键/值 - Recursively check for any null / undefined keys / values in an object using Javascript JavaScript 检查 null 与 undefined 以及 == 和 === 之间的区别 - JavaScript checking for null vs. undefined and difference between == and === 在 JavaScript 中,使用 `a == null` 实际上等同于 `a === null || a === 未定义`? - In JavaScript, is using `a == null` in fact the same as `a === null || a === undefined`? 是否可以让解释器知道我的方法在 TypeScript 中检查 undefined/null - Is it possible to let the interpreter know my method checks for undefined/null in TypeScript JavaScript中的null和undefined之间的区别? - difference between null and undefined in JavaScript? 使用|| 在javascript中执行null / undefined检查? - Using || to perform null / undefined check in javascript? 如何使用 Lodash 添加未定义的检查? - How to add Undefined checks using Lodash? 适用于 null 和 undefined 的 Typescript/javascript 扩展方法 - Typescript/javascript extension method that works with null and undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM