Javascript 如果条件为布尔值

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

Javascript if condition on boolean

javascript

提问by user1052591

Can you explain why the if condition doesn't work without the eval function:

你能解释为什么 if 条件在没有 eval 函数的情况下不起作用:

var myBoolean= document.getElementById("someBoolean").value;  //This is a 'false'

if(myBoolean)
{ 
  alert(Your boolean is True);  //This condition always getting executed even though myBoolean is false;
}

if(eval(myBoolean))
{
 alert("You will never see this alert bcoz boolean is false");
}

回答by Davy8

In Javascript the following values are treated as falsefor conditionals:

在 Javascript 中,以下值被视为false条件:

  • false
  • null
  • undefined
  • The empty string ''
  • The number 0
  • The number NaN
  • false
  • null
  • undefined
  • 空字符串 ''
  • 号码 0
  • 号码 NaN

Everything else is treated as true.

其他一切都被视为true.

'false'is none of the above, so it's true.

'false'以上都不是,所以是true

回答by jamesfzhang

The string 'false'evaluates to the boolean true

字符串'false'计算为布尔值true

回答by Rocket Hazmat

This is because it's not actually a boolean, it's a the string 'false'. When you convert a string to a boolean, ''is falseand anything else is true.

这是因为它实际上不是布尔值,而是字符串'false'。当您将字符串转换为布尔值时,''isfalse和其他任何内容都是true

You check if it's equal to the string 'false'(or 'true') or not.

您检查它是否等于字符串'false'(或'true')。

var myBoolean = 'false'; // (string)
myBoolean = myBoolean !== 'false'; //false (boolean)

回答by AlexC

'false' == true, crazily enough because of JavaScript's implicit type coercion. Check out these other examples from Crockford's The Elements of JavaScript Style.

'false' == true,因为 JavaScript 的隐式类型强制转换已经足够疯狂了。查看Crockford 的 The Elements of JavaScript Style 中的其他示例。

'' == '0' // false
0 == '' // true

0 == '0' // true

false == 'false' // false
false == '0' // true

false == undefined // false
false == null // false

null == undefined // true

' \t\r\n ' == 0 // true

'' == '0' // 假
0 == '' // 真

0 == '0' // 真

false == 'false' // false
false == '0' // true

false == undefined // false
false == null // false

null == 未定义 // 真

' \t\r\n ' == 0 // 真

You could solve this particular problem by changing your code to something like

您可以通过将代码更改为类似的内容来解决此特定问题

var myBoolean = document.getElementById("someBoolean").value === "true"

Also, it is almost always better to use !! and === rather than ! and ==

此外,使用几乎总是更好!和 === 而不是 ! 和 ==

回答by Boundless

Try:

尝试:

var myBoolean= document.getElementById("someBoolean").value;  //This is a 'false'

if(myBoolean != "false")
{ 
  alert(Your boolean is True);  //This condition always getting executed even though myBoolean is false;
}

Like others have said, a string isn't a boolean value, so using it as though it was will give you a logical error.

就像其他人所说的那样,字符串不是布尔值,因此像使用它一样使用它会给您带来逻辑错误。

回答by Tim Wickstrom

document.getElementById("someBoolean") does not return a boolean true/false it returns an element or undefined / null

document.getElementById("someBoolean") 不返回布尔值 true/false 它返回一个元素或 undefined / null

you could reverse your logic and get the expected result:

你可以颠倒你的逻辑并得到预期的结果:

    if(!myBoolean)
    { 
      alert('This element does not exist');
    }

    if(!eval(myBoolean))
    {
     alert("Do not know why you would ever want to do this");
// you could do typeof() 
    }

回答by duckisland

A string IS a boolean truth value according to ECMA

根据 ECMA,字符串是布尔真值

var a = ""<br/>
a&&false //-> false<br/>
a||false //-> "" (truthy)<br/>