Javascript 检查是否为假

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10139588/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:02:00  来源:igfitidea点击:

Check for false

javascriptajaxif-statement

提问by Alex

Is there any better way of doing this?

有没有更好的方法来做到这一点?

if(borrar() !== false)
{
    alert('tatatata bum bum bum prapra');
}
return false;

采纳答案by Phil H

If you want to check for false and alert if not, then no there isn't.

如果你想检查 false 和 alert 如果没有,那么没有。

If you use if(val), then anything that evaluates to 'truthy', like a non-empty string, will also pass. So it depends on how stringent your criterion is. Using ===and !==is generally considered good practise, to avoid accidentally matching truthy or falsy conditions via javascript's implicit boolean tests.

如果您使用if(val),则任何评估为“真实”的内容(如非空字符串)也将通过。所以这取决于你的标准有多严格。使用===and!==通常被认为是一种很好的做法,以避免通过 javascript 的隐式布尔测试意外匹配真或假条件。

回答by GillesC

If you want an explicit check against false (and not undefined, null and others which I assume as you are using !== instead of !=) then yes you have to use that. also this is the same in a slightly smaller footprint

如果您想要对 false(而不是 undefined、null 和我假设的其他人使用 !== 而不是 !=)进行显式检查,那么是的,您必须使用它。这也是相同的,占用空间略小

if(borrar() !== !1)

回答by Dean Gite

You can use something more simpler :)

你可以使用更简单的东西:)

if(!var){
  console.log('var is false'); 
}

回答by André Silva

Checking if something isn't false.. So it's true, just if you're doing something that is quantum physics.

检查某事是否不是假的.. 所以这是真的,只要你在做一些量子物理学的事情。

if(!(borrar() === false))

or

或者

if(borrar() === true)

回答by Mark Walters

Like this?

像这样?

if(borrar())
{
   //do something
}

If borrar()returns true then do something (if it is not false).

如果borrar()返回 true 然后做一些事情(如果它不是 false)。

回答by Griffin

If you want it to check explicit for it to not be false (boolean value) you have to use

如果您希望它明确检查它是否为假(布尔值),则必须使用

if(borrar() !== false)

如果(borrar()!== 假)

But in javascript we usually use falsy and truthy and you could use

但是在javascript中我们通常使用falsy和truthy,你可以使用

if(!borrar())

如果(!borrar())

but then values 0, '', null, undefined, null and NaN would not generate the alert

但是然后值 0、''、null、undefined、null 和 NaN 不会生成警报

The following values are always falsy: false, ,0 (zero) ,'' or "" (empty string) ,null ,undefined ,NaN

以下值始终为假:false, ,0 (零) ,'' 或 "" (空字符串) ,null ,undefined ,NaN

Everything else is truthy. That includes: '0' (a string containing a single zero) ,'false' (a string containing the text “false”) ,[] (an empty array) ,{} (an empty object) ,function(){} (an “empty” function)

其他一切都是真实的。这包括: '0'(包含单个零的字符串)、'false'(包含文本“false”的字符串)、[](空数组)、{}(空对象)、function(){} (一个“空”函数)

Source https://www.sitepoint.com/javascript-truthy-falsy/

来源https://www.sitepoint.com/javascript-truthy-falsy/

As an extra perk to convert any value to true or false (boolean type) use double exclamation mark.

作为将任何值转换为 true 或 false(布尔类型)的额外好处,请使用双感叹号。

!![] === true
!!'false' === true
!!false === false
!!undefined === false