Javascript javascript检查非空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2422946/
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
javascript check for not null
提问by ria
Below is a code snippet, where we retrieve a form value. Before further processing check if the value is not null..
下面是一个代码片段,我们在其中检索表单值。在进一步处理之前检查该值是否不为空..
var val = document.FileList.hiddenInfo.value;
alert("val is " + val); // this prints null which is as expected
if (val != null)
{
alert("value is "+val.length); // this returns 4
}
else
{
alert("value* is null");
}
Any ideas why it happens so.. ??
任何想法为什么会这样......?
采纳答案by Arsen Mkrtchyan
It's because val is not null, but contains 'null'as a string.
这是因为 val 不是null,而是包含'null'为字符串。
Try to check with 'null'
尝试检查“空”
if ('null' != val)
For an explanation of when and why this works, see the details below.
有关何时以及为何起作用的说明,请参阅下面的详细信息。
回答by user144390
this will do the trick for you
这对你有用
if (!!val) {
alert("this is not null")
} else {
alert("this is null")
}
回答by tfmontague
There are 3 ways to check for "not null". My recommendation is to use the Strict Not Version.
有 3 种方法可以检查“非空”。我的建议是使用 Strict Not Version。
1. Strict Not Version
1.严格不是版本
if (val !== null) { ... }
The Strict Not Version uses the "Strict Equality Comparison Algorithm" http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6. The !==has faster performance, than the !=operator because the Strict Equality Comparison Algorithm doesn't typecast values.
Strict Not 版本使用“严格平等比较算法” http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6。将!==有更快的性能,比!=因为全等比较算法没有做强制转换值操作。
2. Non-strict Not Version
2.非严格非版本
if (val != 'null') { ... }
The Non-strict version uses the "Abstract Equality Comparison Algorithm" http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3. The !=has slower performance, than the !==operator because the Abstract Equality Comparison Algorithm typecasts values.
非严格版本使用“抽象平等比较算法” http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3。的!=性能比!==运算符慢,因为抽象相等比较算法对值进行类型转换。
3. Double Not Version
3.双不版本
if (!!val) { ... }
The Double Not Version !!has faster performance, than both the Strict Not Version !==and the Non-Strict Not Version !=(https://jsperf.com/tfm-not-null/6). However, it will typecast "Falsey" values like undefinedand NaNinto False (http://www.ecma-international.org/ecma-262/5.1/#sec-9.2) which may lead to unexpected results, and it has worse readability because nullisn't explicitly stated.
Double Not 版本!!比 Strict Not Version!==和 Non-Strict Not Version !=( https://jsperf.com/tfm-not-null/6)具有更快的性能。然而,它会强制转换“Falsey”的价值观一样undefined,并NaN为假(http://www.ecma-international.org/ecma-262/5.1/#sec-9.2),这可能导致意外的结果,它具有可读性变得更糟,因为null没有明确说明。
回答by Plynx
Use !==as !=will get you into a world of nontransitive JavaScript truth table weirdness.
使用!==as!=会让你进入一个非传递性 JavaScript 真值表怪异的世界。
回答by Warty
You should be using the strict not equalscomparison operator !==so that if the user inputs "null"then you won't get to the else.
您应该使用严格not equals比较运算符,!==这样如果用户输入,"null"那么您将不会到达else.
回答by ScottyUCSD
It is possibly because the value of valis actually the string "null"rather than the value null.
这可能是因为 的值val实际上是字符串"null"而不是值null。
回答by vkGunasekaran
This should work fine..
这应该工作正常..
if(val!= null)
{
alert("value is "+val.length); //-- this returns 4
}
else
{
alert("value* is null");
}
回答by punkrockpolly
If you want to be able to include 0 as a valid value:
如果您希望能够包含 0 作为有效值:
if (!!val || val === 0) { ... }
回答by shajivk
This will work:
这将起作用:
if (val) {
alert("Not null");
} else {
alert("Null");
}
回答by Feras
Check https://softwareengineering.stackexchange.com/a/253723
检查https://softwareengineering.stackexchange.com/a/253723
if(value) {
}
will evaluate to true if value is not:
如果 value 不是,则评估为 true:
null
undefined
NaN
empty string ("")
0
false

