简体   繁体   English

无法在js中的条件语句中比较0

[英]cant compare 0 in a conditional statement in js

well this is in node to be exact but, when i do the following conditional statement on a string of a length of zero or above zero, it will only ever do the condition if the string is not 0. 好吧,这在节点上是准确的,但是,当我在长度为零或大于零的字符串上执行以下条件语句时,只有在字符串不为0时才会执行条件。

if(string.length == 0)
{
   console.log(string.length)
} else {
   console.log(string.length)
};

no matter what it wont respond if the string.length is 0. i have also tried: 无论string.length是否为0,它都不会响应。我也尝试过:

if(string.length > 0)
{
    console.log('greater than zero')
} else {
    console.log('zero')
};

but with the same results. 但结果相同。 no matter what it wont respond if the string.length is zero. 无论string.length为零,它不会响应什么。 i have printed the string.length outside of the if/else statement, and have been able to have it print 0. why wont the conditional statement work in this case? 我已经在if / else语句外部打印了string.length,并且能够将其打印为0。在这种情况下,为什么条件语句不起作用? why does it just not respond if the string.length == 0? 为什么string.length == 0只是不响应?

Seems to work. 似乎可以工作。 Fiddle here Firebug records a 0 if there is nothing in the <div> 如果<div>没有任何内容,Firebug会在此处拨弄小号

如果字符串为0,我将获得空格。我通过执行string.trim()解决了该错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM