Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:为什么“if(element!=''| | element!=null)”不起作用?_Javascript - Fatal编程技术网

Javascript:为什么“if(element!=''| | element!=null)”不起作用?

Javascript:为什么“if(element!=''| | element!=null)”不起作用?,javascript,Javascript,我的脚本中有这段代码 var therow; var rowtitle = ['Name', 'Weight']; for(var i=0;i<7;i++) { therow = prompt(rowtitle[i]); if(therow != '' || therow != null) { //some code } else { //more code } therow = null; } 我知道这一点,因为

我的脚本中有这段代码

var therow;
var rowtitle = ['Name', 'Weight'];
for(var i=0;i<7;i++) {
    therow = prompt(rowtitle[i]);
    if(therow != '' || therow != null) {
           //some code
    } else {
          //more code
    }
therow = null;
}
我知道这一点,因为我已经试过了

if(therow != '')

…独立,他们的行为符合预期

为什么当我把上面两个结合在一个if语句中时,它什么也不做


上面的代码有什么问题吗?

我会使用&&。您希望它不为null,也不为空,对吗?

我会使用&&。您希望它不为null且不为空,对吗?

因为它始终为真

您已经说过,如果它不是空字符串,或者它不是空字符串。当它为NULL时,它不是一个空白字符串,因此为true。当它是一个空白字符串时,它不是空的,所以它是真的


你想要的是如果现在therow!=如果为空,则更可能为空。我也看到了如果!!它强制它成为一个实际的bolean值。

,因为它始终是真的

您已经说过,如果它不是空字符串,或者它不是空字符串。当它为NULL时,它不是一个空白字符串,因此为true。当它是一个空白字符串时,它不是空的,所以它是真的

你想要的是如果现在therow!=如果为空,则更可能为空。我也看到了如果!!therow,强制将其转换为实际的bolean值。

尝试使用以下方法:

if (!!therow){
           //some code
    } else {
          //more code
    }
这是一种较短的方法

尝试使用以下方法:

if (!!therow){
           //some code
    } else {
          //more code
    }

这是一种较短的方式

用于转换Row!=||therow!=null到therow==&&therow==null并研究转换。如何可以同时使用row和null?

用于转换row!=||therow!=null到therow==&&therow==null并研究转换。怎么能同时显示和显示空值?

你能把它复制成小提琴让我们看一下吗?你可以用if!TheRow你试过console.logtherow吗?你可能是指&&而不是| |,因为0===undefined==null==NaN==false use===你能用小提琴把它复制出来让我们看看吗?你可以用if!therow你试过console.logtherow吗?你可能是指&&而不是| |,因为0===undefined==null==NaN==false use===但这仍然不起作用,因为我们不知道QWhat在therow中。这不是他要问的,这也不是一个答案。事实上,我希望它不为空或不为空。但这仍然不起作用,因为我们不知道下面是什么。这不是他要问的,这也不是一个答案。事实上,我希望它不为空或不为空。从技术上讲,如果它为空,它将是错误的undefined@SimonBelanger这条路实际上是被定义的。@DavidHeisnam你是对的。prompt从不返回未定义的。对不起,我发现这个问题很难理解。但我现在明白了。非常感谢。从技术上讲,如果现在是这样的话,那就错了undefined@SimonBelanger这条路实际上是被定义的。@DavidHeisnam你是对的。prompt从不返回未定义的。对不起,我发现这个问题很难理解。但我现在明白了。非常感谢。对于最后一个问题,这就是我使用OR的原因。对于最后一个问题,这就是我使用OR的原因。
if (!!therow){
           //some code
    } else {
          //more code
    }