本文介绍了为什么使用if(!! err)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
来自的代码示例:
if (!!err) {
console.log('Unable to connect to the database:', err)
} else {
console.log('Connection has been established successfully.')
}
(!! err)来测试这个错误的真实性?是否与相同如果(错误)
?
Why are they using (!!err)
to test that err's truthiness? Isn't that the same as if (err)
?
推荐答案
没有理由。也许他们过于谨慎,听到一些关于无辜的错误的事情?或者他们想要强调发生在评估如果
条件?
There's no reason. Maybe they're overcautious, having heard some wrong things about thruthiness? Or they want to emphasize the ToBoolean
cast that occurs in the evaluation of the if
condition?
是。
if (err)
if (Boolean(err))
if (!! err)
所有意味着完全相同的事情。后两者在达到相同结果之前只做了不必要的步骤。
all mean exactly the same thing. The latter two only doing unnecessary steps in between before arriving at the same result.
这篇关于为什么使用if(!! err)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!